This is a very simple cURL wrapper for PHP.
$http = new HTTP( [array $options] );
Parameter | Type | Description |
---|---|---|
userAgent | string | User agent string. |
timeout | int | Value in seconds the HTTP will time out. |
referer | string | A HTTP referral. |
headers | array | Any custom headers. |
username | string | Username for basic HTTP authentication. |
password | string | Password for basic HTTP authentication. |
// Include core HTTP library
require_once 'class.HTTP.php';
// Initialize HTTP object
$http = new HTTP([
// User agent for Google Chrome
'userAgent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
// Request timed out in 60 seconds
'timeout' => 60,
// Simulate we clicked from https://www.google.com
'referer' => 'https://www.google.com',
]);
Gets logs for debugging purpose.
array $http->getLogs( );
$logs = $http->getLogs();
echo '<pre>';
print_r($logs);
echo '</pre>';
Sends a HTTP GET request.
array $http->get( string $url );
// Visit to https://www.reddit.com
$response = $http->get('https://www.reddit.com');
if ($response) {
echo $response['header'];
echo $response['body'];
}
Sends a HTTP POST request.
array $http->post( string $url, [array $fields] );
// Simulate a form post for login at Reddit website
$response = $http->post('https://www.reddit.com/api/login/', [
'op' => 'login-main',
'user' => 'test',
'passwd' => 'Gmxn3$dcSD',
'api_type' => 'json',
]);
if ($response) {
echo $response['header'];
echo $response['body'];
}
Downloads a file
array $http->download( string $url, string $path );
$response = $http->download('https://github.com/seikan/HTTP/archive/master.zip', '/var/www/');
if ($response) {
echo 'File "'.$response['file'].'" with size '.$response['size'].' bytes has been downloads.';
}
Uploads a file.
array $http->upload( string $url, [array $fields], array $files );
$response = $http->upload('https://www.example.com/upload', [], [
'attachment' => '/var/www/example.zip',
]);
Uses HTTP proxy server.
$http->useProxy( string $host, string $port, string $username, string $password );
$http->useProxy('127.0.0.1', 3128, 'proxy_user', 'proxy_password');
$response = $http->get('https://www.whatismyip.com/');