dev-master
9999999-devA basic CURL wrapper for PHP
MIT
The Requires
- php >=5.4.0
The Development Requires
by Ali OYGUR
v1.0
1.0.0.0A basic CURL wrapper for PHP
MIT
The Requires
- php >=5.4.0
The Development Requires
by Ali OYGUR
A basic CURL wrapper for PHP
A basic CURL wrapper for PHP (see http://php.net/curl for more information about the libcurl extension for PHP), (*1)
This library is available via Composer, (*2)
{ "require": { "alioygur/curl": "~1.0" } }
Simply initialize and usage the Curl
class like so:, (*3)
<?php use Alioygur\Curl\Curl; $curl = new Curl(); $response = $curl ->setOption('CURLOPT_FOLLOW_REDIRECTS', false) ->setHeader('User-Agent', 'My Name is Heisenberg!') ->get('http://example.com');
The Curl object supports 5 types of requests: HEAD, GET, POST, PUT, and DELETE. You must specify a url to request and optionally specify an associative array or string of variables to send along with it., (*4)
$response = $curl->head($url, $vars = []); $response = $curl->get($url, $vars = []); # The Curl object will append the array of $vars to the $url as a query string $response = $curl->post($url, $vars = []); $response = $curl->put($url, $vars = []); $response = $curl->delete($url, $vars = []);
To use a custom request methods, you can call the request
method:, (*5)
$response = $curl->request('YOUR_CUSTOM_REQUEST_TYPE', $url, $vars = []);
All of the built in request methods like put
and get
simply wrap the request
method. For example, the post
method is implemented like:, (*6)
function post($url, $vars = []) { return $this->request('POST', $url, $vars); }
Examples:, (*7)
$response = $curl->get('google.com?q=test'); # The Curl object will append '&some_variable=some_value' to the url $response = $curl->get('google.com?q=test', array('some_variable' => 'some_value')); $response = $curl->post('test.com/posts', array('title' => 'Test', 'body' => 'This is a test'));
All requests return a CurlResponse object (see below) or false if an error occurred. You can access the error string with the $curl->error()
method., (*8)
A normal CURL request will return the headers and the body in one response string., (*9)
# Response Headers ------------------------------------------------------------------------- # Get the response body echo $response->body(); # A string containing everything in the response except for the headers # Get the response headers print_r($response->headers()); # An associative array containing the response headers # Pick one from response headers echo $response->headers('Content-Type'); # text/html # You can also use those methods $response->status(); # 200 OK $response->statusCode(); # 200 $response->ContentType(); # text/html # Request Headers -------------------------------------------------------------------------- # Get the request headers $response->requestHeaders(); # An associative array containing the request headers # Pick one from request headers echo $response->requestHeaders('Version'); # HTTP/1.1 # Curl Information ------------------------------------------------------------------------- Get information regarding a specific transfer. See, http://php.net/manual/en/function.curl-getinfo.php # Get all $response->getInfo(); # An associative array containing the curl information # Pick one $response->getInfo('total_time'); # 0.14257
The CurlResponse class defines the magic __toString() method which will return the response body, so echo $response
is the same as echo $response->body
, (*10)
By default, cookies will be stored in a file called curl_cookie.txt
. You can change this file's name by setting it like this, (*11)
$curl->setCookieFile('some_other_filename');
This allows you to maintain a session across requests, (*12)
You can set custom headers to send with the request, (*13)
$curl->setheader('SOME_KEY', 'some value'); # you can also method chaining $response = $curl->setHeader('Content-Type', 'application/json') ->setHeader('User-Agent', 'Mozilla/5.0 (X11; Linux...') ->get('http://example.com');
By default, the Curl
object will follow redirects. You can disable this by setting:, (*14)
$curl->setOptions('CURLOPT_FOLLOW_REDIRECTS', false);
You can set/override many different options for CURL requests (see the curl_setopt() documentation for a list of them), (*15)
Sets the user and password for HTTP auth basic authentication method., (*16)
$curl->setAuth('username', 'password');
Problems, comments, and suggestions all welcome: alioygur@gmail.com, (*17)
A basic CURL wrapper for PHP
MIT
A basic CURL wrapper for PHP
MIT