1. Go to this page and download the library: Download originphp/http-client 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/ */
originphp / http-client example snippets
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->get('https://api.example.com/posts');
// To use query parameters. https://api.example.com/posts?api_token=1234-1234-1234-1234
$response = $http->get('https://api.example.com/posts',[
'query' => ['api_token' => '1234-1234-1234-1234']
]);
// An example with more options
$response = $http->get('https://api.example.com/posts',[
'headers' => ['Accept' => 'application/json'],
'cookies' => ['name' => 'value'],
'userAgent' => 'MyApp',
'referer' => 'https://www.somewebsite.com/search'
]);
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->head('https://api.example.com/posts');
// To use query parameters. https://api.example.com/posts?api_token=1234-1234-1234-1234
$response = $http->head('https://api.example.com/posts',[
'query' => ['api_token' => '1234-1234-1234-1234']
]);
// An example with more options
$response = $http->head('https://api.example.com/posts',[
'headers' => ['Accept' => 'application/json'],
'cookies' => ['name' => 'value'],
'userAgent' => 'MyApp',
'referer' => 'https://www.somewebsite.com/search'
]);
use Origin\HttpClient\Http;
$http = new Http();
// to send a post request with empty data
$response = $http->post('https://api.example.com/posts');
// to send a post request with data
$response = $http->post('https://api.example.com/posts',[
'data' => [
'title' => 'Article Title',
'body' => 'Article body'
]
]);
// example with other options
$response = $http->post('https://api.example.com/posts',[
'data' => [
'title' => 'Article Title',
'body' => 'Article body'
],
'headers' => ['Accept' => 'application/json'],
'cookies' => ['name' => 'value'],
'userAgent' => 'MyApp',
'referer' => 'https://www.somewebsite.com/search'
]);
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->patch('https://api.example.com/posts/1',[
'data' => [
'title' => 'Another Article Title',
],
]);
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->delete('https://api.example.com/posts/1');
// Example passing some options
$response = $http->delete('https://api.example.com/posts/1',[
'userAgent' => 'OriginPHP'
]);
use Origin\HttpClient\Http;
$http = new Http([
'base' => 'https://www.example.com/api'
]);
$response = $http->get('/posts');
$http = new Http([
'httpErrors' => false
]);
try {
(new Http())->get('http://wwww.google.com');
} catch (HttpException $exception) {
// do something
}
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->head('https://api.example.com/posts');
// working with the response body
$data = $response->body();
$data = $response->json(); // decodes a JSON response into an array
$data = $response->xml(); // converts XML response into an array
// Response stuff
$headers = $response->headers(); // headers
$cookies = $response->cookies(); // cookies from the *response*
$code = $response->statusCode();
// Assertions
$bool = $response->ok(); // has status code of 200
$bool = $response->success(); // has any 20x status code
$bool = $response->redirect(); // has a redirect status code 30x
use Origin\HttpClient\Http;
// Persist cookies for this session only (stores in array)
$http = new Http([
'cookieJar' => true
]);
// Don't use cookies
$http = new Http([
'cookieJar' => false;
]);
// Persist cookies to file
$http = new Http([
'cookieJar' => '/var/www/data/cookies.data';
]);