Just another PHP cURL wrapper
As you can see, this library contain composer.json
file. But at this time, i have not post this library on packagist. So if you want to use it via composer, you can manually add this repository in your composer.json
file.
So, your composer.json
file should look like this:
{
"require": {
"rakit/curl": "dev-master"
},
"repositories": [
{
"url": "https://github.com/emsifa/rakit-curl",
"type": "vcs"
}
]
}
Then you can run composer install
or composer update
.
Optionally you can download/clone this library and load it into your project.
use Rakit\Curl\Curl;
$request = new Curl('http://wikipedia.com');
$response = $request->get();
// then, you can do something with response object
if(! $response->error()) {
// getting response file content
$html = $response->getBody();
// simply get response content type
$content_type = $response->getContentType();
// get content type via curl info
$content_type = $response->getInfo('content_type');
// getting response cookies
$cookie = $response->getCookie();
// simply get response header item
$http_version = $response->getHeader('http_version');
// get all header items
$all_headers = $response->getHeaders();
} else {
$error_code = $response->getErrno();
$error_message = $response->getErrorMessage();
}
use Rakit\Curl\Curl;
$request = new Curl('http://targetsite.com/products');
$request->param('page', 2);
$request->param('category', 'software');
$response = $request->get();
// do something with Response object
or
use Rakit\Curl\Curl;
$params = array(
'page' => 2,
'category' => 'software'
);
$request = new Curl('http://targetsite.com/products');
$response = $request->get($params);
// do something with Response object
use Rakit\Curl\Curl;
$request = new Curl('http://targetsite.com/register');
$request->param('name', 'John Doe');
$request->param('email', 'johndoe@mail.com');
$request->param('password', '12345');
$response = $request->post();
// do something with Response object
use Rakit\Curl\Curl;
$request = new Curl('http://targetsite.com/register');
$request->param('name', 'John Doe');
$request->param('email', 'johndoe@mail.com');
$request->param('password', '12345');
$request->addFile('avatar', 'path/to/avatar.png');
$response = $request->post();
// do something with Response object
use Rakit\Curl\Curl;
$request = new Curl('http://targetsite.com/products');
$request->cookie('key', 'value');
$response = $request->get();
// do something with Response object
Auto redirect allow cURL to auto redirect while response is redirection (status: 3xx).
Example:
use Rakit\Curl\Curl;
$request = new Curl('http://targetsite.com/admin/product/delete/1');
$request->autoRedirect(5); // 5 is maximum redirection
$response = $request->get();
// what if 6th request is also redirection? it's good to check
if($response->isRedirect()) {
throw new \Exception("Too many redirection");
}
// do something with response
It is easy way to store session automatically with CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE. It is useful when you want to grab something that require session cookies to login or anything else. For use this, you must have directory that have access to write file for storing session.
For example you want grab redirected page after login:
use Rakit\Curl\Curl;
$session_file = "path/to/store/sitetarget.session";
$request = new Curl("http://sitetarget.com/login");
$request->autoRedirect(5);
$request->storeSession($session_file);
$response = $request->post(array(
'username' => 'my_username',
'password' => 'my_password'
));
// do something with response object
With simple request, you dont't need to create new Curl request object. But you can't send a cookie, modify request headers, or even curl options.
use Rakit\Curl\Curl;
// simple GET
$response = Curl::doGet('http://targetsite.com', array('page' => 2));
// simple POST
$response = Curl::doPost('http://targetsite.com/product/delete', array('id' => 5));
In examples above, i always told you to do something with response object, but what it is? what you can do with response object?
Response object is object that returned from your cURL request. Response object contain result data from HTTP response such as response headers, cookies, body, etc.
Here is that you can do with response object:
Shortcut for check response status code is 404 or not.
$response = Curl::doGet("http://sitetarget.com/blablabla");
if($response->isNotFound()) {
// 404 page not found
}
Return true if status code is 3xx.
// redirecting example
$response = Curl::doGet("http://sitetarget.com/redirect-me");
while($response->isRedirect()) {
$redirect_url = $response->getHeader("location");
$response = Curl::doGet($redirect_url);
}
Getting response body like html code, image content, etc.
Getting response length (response body string + response header string).
Return true if response content-type is text/html.
Return true if there is error in request
Getting error code, 0 = no error.
Getting error message, empty if no error
Getting header item
$response = Curl::doGet("http://sitetarget.com");
$http_version = $response->getHeader("http_version");
$content_type = $response->getHeader("content-type");
// and many more
Getting all header items as array.
Get response headers as raw header string.
Getting response status code.
Shortcut for getting response content-type.
Getting info item like curl_getinfo()
Getting all info as array.