PHP code example of tcdent / php-restclient
1. Go to this page and download the library: Download tcdent/php-restclient library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tcdent / php-restclient example snippets php
$api = new RestClient([
'base_url' => "https://api.twitter.com/1.1",
'format' => "json",
// https://dev.twitter.com/docs/auth/application-only-auth
'headers' => ['Authorization' => 'Bearer '.OAUTH_BEARER],
]);
$result = $api->get("search/tweets", ['q' => "#php"]);
// GET http://api.twitter.com/1.1/search/tweets.json?q=%23php
if($result->info->http_code == 200)
var_dump($result->decode_response());
php
$api = new RestClient;
$api->set_option('format', "json");
$api->set_option('user_agent', "my-application/0.1");
php
$response->headers->content_type;
$response->headers->x_powered_by;
php
$api = new RestClient([
'base_url' => "http://vimeo.com/api/v2",
'format' => "php"
]);
$result = $api->get("tcdent/info");
// GET http://vimeo.com/api/v2/tcdent/info.php
foreach($result as $key => $value)
var_dump($value);
php
var_dump($result['id']);
php
function my_xml_decoder($data){
new SimpleXMLElement($data);
}
$api = new RestClient([
'format' => "xml",
'decoders' => ['xml' => "my_xml_decoder"]
]);
php
$api = new RestClient;
$api->set_option('format', "xml");
$api->register_decoder('xml', "my_xml_decoder");
php
$api->register_decoder('json', function($data){
return json_decode($data, TRUE);
});
php
$result = $api->get('/');
var_dump($result->headers->content_type);
=> ["text/html", "text/html; charset=UTF-8"]
php
$result = $api->get('/', [
'foo[]' => ['bar', 'baz']
], [
'Accept' => ['text/json', 'application/json']
]);
php
$result = $api->get('/');
var_dump($result->response_status_lines);
=> ["HTTP/1.1 100 Continue", "HTTP/1.1 200 OK"]
php
$result = $api->execute("http://httpbin.org/patch", 'PATCH',
json_encode([foo' => 'bar']), [
'X-HTTP-Method-Override' => 'PATCH',
'Content-Type' => 'application/json-patch+json']);
sh
$ php -S localhost:8888 RestClientTest.php
$ phpunit RestClientTest.php