1. Go to this page and download the library: Download hightman/httpclient 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/ */
hightman / httpclient example snippets
use hightman\http\Client;
$http = new Client();
// 1. display response contents
echo $http->get('http://www.baidu.com');
echo $http->get('http://www.baidu.com/s', ['wd' => 'php']);
// 2. capture the response object, read the meta information
$res = $http->get('http://www.baidu.com');
print_r($res->getHeader('content-type'));
print_r($res->getCookie(null));
// 3. post request
$res = $http->post('http://www.your.host/', ['field1' => 'value1', 'field2' => 'value2']);
if (!$res->hasError()) {
echo $res->body; // response content
echo $res->status; // response status code
}
// 4. head request
$res = $http->head('http://www.baidu.com');
print_r($res->getHeader(null));
// delete request
$res = $http->delete('http://www.your.host/request/uri');
// 5. restful json requests
// there are sismilar api like: postJson, putJson
$data = $http->getJson('http://www.your.host/request/uri');
print_r($data);
$data = $http->postJson('http://www.your.host/reqeust/uri', ['key1' => 'value1', 'key2' => 'value2']);
use hightman\http\Client;
use hightman\http\Request;
$http = new Client();
$request = new Request('http://www.your.host/request/uri');
// set method
$request->setMethod('POST');
// add headers
$request->setHeader('user-agent', 'test robot');
// specify host ip, this will skip DNS resolver
$request->setHeader('x-server-ip', '1.2.3.4');
// add post fields
$request->addPostField('name', 'value');
$request->addPostFile('upload', '/path/to/file');
$request->addPostFile('upload_virtual', 'virtual.text', 'content of file ...');
// or you can specify request body directly
$request->setBody('request body ...');
// you also can specify JSON data as request body
// this will set content-type header to 'application/json' automatically.
$request->setJsonBody(['key' => 'value']);
// specify context options of connect, such as SSL options
$request->contextOptions = [
'ssl' => ['verify_peer_name' => false, 'local_cert' => '/path/to/file.pem'],
];
// execute the request
$response = $http->exec($request);
print_r($response);
use hightman\http\Client;
use hightman\http\Request;
use hightman\http\Response;
// Define callback as function, its signature:
// (callback) (Response $res, Request $req, string|integer $key);
function test_cb($res, $req, $key)
{
echo '[' . $key . '] url: ' . $req->getUrl() . ', ';
echo 'time cost: ' . $res->timeCost . ', size: ' . number_format(strlen($res->body)) . "\n";
}
// or you can define callback as a class implemented interface `ParseInterface`.
class testCb implements \hightman\http\ParseInterface
{
public function parse(Response $res, Request $req, $key)
{
// your code here ...
}
}
// create client object with callback parser
$http = new \hightman\http\Client('test_cb');
// or specify later as following
$http->setParser(new testCb);
// Fetch multiple URLs, it returns after all requests are finished.
// It may be slower for the first time, because of DNS resolover.
$results = $http->mget([
'baidu' => 'http://www.baidu.com/',
'sina' => 'http://news.sina.com.cn/',
'qq' => 'http://www.qq.com/',
]);
// show all results
// print_r($results);
$http->setCookiePath('/path/to/file');
$http->setHeader('authorization', 'Bearer ' . $token);
// or add header for request object
$request->setHeader('authorization', 'Bearer ' . $token);
// use socks5
Connection::useProxy('socks5://127.0.0.1:1080');
// use socks5 with username & password
Connection::useProxy('socks5://user:[email protected]:1080');
// use HTTP proxy
Connection::useProxy('http://127.0.0.1:8080');
// use HTTP proxy with basic authentication
Connection::useProxy('http://user:[email protected]:8080');
// use socks4 proxy
Connection::useProxy('socks4://127.0.0.1:1080');
// disable socks
Connection::useProxy(null);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.