What is SimpleCurl
SimpleCurl is the easiest way to do server to server communication using cURL., (*1)
Requirements
PHP 5+, cURL enabled, (*2)
Features
- Easy to use
- Super configurable
- Autoparse response
Usage
<?php
require 'src/autoload.php';
$c = new \Perecedero\SimpleCurl\Caller();
$result = $c->call(array(
//Options here
));
if ($result->code == 200) {
$parsed_body = $result->get();
//do something
} else {
//debug
print_r($result->get('headers.sent') );
print_r($result->get('headers.rcvd'));
print_r($result->get('body')); //raw response body
}
===, (*3)
Complete call options list, (*4)
Response object reference, (*5)
===, (*6)
Examples
Make a GET Request, (*7)
<?php
require 'src/autoload.php';
$c = new \Perecedero\SimpleCurl\Caller(array(
'url.domain' => 'https://api.twitter.com',
'parse.body.onerror' => true,
'parse.body' => 'json',
));
$res = $c->call(array(
'url.path' => '/1.1/search/tweets.json?q=@twitterapi',
));
if ($res->code != 200) {
$errors = $res->get()->errors;
print_r($errors);
}
Make a POST Request, (*8)
<?php
require 'src/autoload.php';
$c = new \Perecedero\SimpleCurl\Caller(array(
'url.domain' => 'http://wordpress.org',
'parse.body.onerror' => true,
'parse.body' => 'json',
));
$res = $c->call(array(
'url.path' => '/search/do-search.php',
'post' => array('search'=> 'SimpleCurl')
));
===, (*9)
Options list
url:
URL to make the request
* type string
* REQUIRED, (*10)
url.domain:
URL to make the request
* type string
* REQUIRED if not passed option url, (*11)
url.path:
URL to use in combination with url.domain
* type string, (*12)
user.pwd:
Login details string for the connection. The format of which is: [user name]:[password]
* type String
* default null, (*13)
method:
A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request
* type String
* default null, (*14)
header:
List of headers to be send
* type array
* default null, (*15)
cookie:
list of cookies to be send
* type mixed (array|string)
* default null, (*16)
proxy:
The HTTP proxy to tunnel requests through. format [host]:[port]
* type string
* default null, (*17)
follow.location:
Follow any Location: header that the server sends as part of a HTTP header in a 3xx response.
* type boolean
* default false, (*18)
verify.ssl
Verify if ssl certification is valid
* type boolean
* default false, (*19)
timeout:
Number of seconds to wait after the communication has been established
* type integer
* default null, (*20)
post:
List of arguments to be send via POST
* type array
* default null, (*21)
upload.file
Path to the file to be send via POST
* type string
* default null, (*22)
save.on
Path to the file to be used to store the output
Also used to download files
* type string
* default null, (*23)
return.body
return call response body instead of boolean as function return
* type boolean
* default true, (*24)
parse.body:
Parse response. valid with return.body=true
* values 'auto', 'xml', 'json', 'json.assoc', 'raw', false
* type mixed
* default 'auto', (*25)
parse.body.onerror:
Parse response on error (4xx or 5xx HTTP code received). valid with return.body=true
* type boolean
* default false, (*26)
===, (*27)
Response Object Reference
You can obtain all the information about the result with the get method., (*28)
Possible method arguments are:, (*29)
- 'code' : HTTP status code received
- 'headers.sent' : list of headers sent on the petition
- 'headers.rcvd' => list of headers received as part of the response
- 'body': Raw response body
- 'parsed.body': Parsed response
- 'latency': time to conclude the petition
- 'size': raw body size
Note: Without any argument this method will return the parsed body, (*30)
$c = new \Perecedero\SimpleCurl\Caller(array(
'url.domain' => 'https://api.twitter.com',
'parse.body.onerror' => true,
'parse.body' => 'json',
));
$res = $c->call(array(
'url.path' => '/1.1/search/tweets.json?q=@twitterapi',
));
print_r ($res->get('headers.rcvd'));
```PHP, (*31)
HTTP/1.1 400 Bad Request
content-length: 62
content-type: application/json;charset=utf-8
date: Mon, 09 Mar 2015 17:59:20 UTC
server: tsa_c
set-cookie: guest_id=v1%3A142592396054742794; Domain=.twitter.com; Path=/; Expires=Wed, 08-Mar-2017 17:59:20 UTC
strict-transport-security: max-age=631138519
x-connection-hash: f6f703b23be46fc71c8d1ddd457e6fbf
x-response-time: 21
You can also use use the __get method to obtain all this values
```PHP
$c = new \Perecedero\SimpleCurl\Caller(array(
'url.domain' => 'https://api.twitter.com',
'parse.body.onerror' => true,
'parse.body' => 'json',
));
$res = $c->call(array(
'url.path' => '/1.1/search/tweets.json?q=@twitterapi',
));
print_r($res->code);
print_r($res->latency);
print_r($res->body);