Because I didn’t want to include any third-party stuff in my raw PHP code I build this simple general-purpose class.
This class works with php5 and php5-curl.
Usage
Initialization
$req = new httpRequest('http://example.org/itemPage.html');
Make a POST request
$req->setPOST($rawPostData);
Set the referrer URL
$req->setReferrerUrl('http://example.org/?search=item');
Set the user agent string
$req->setUserAgent('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.14) '. 'Gecko/20080530 Firefox/2.0.0.14 Flock/1.2.1');
Set a random user agent from a file
$req->userAgentsFile = 'fileName'; // By default 'userAgents' file is used $req->setRandUserAgent();
Set the request timeout in seconds
$req->setTimeout(15); // The default timeout is 30s
Set the file to be used to store and send cookies
$req->setCookieFile('cookiesFile');
Set cookie contents in header
$req->setCookieContents('fruit=apple; colour=red');
Specify wether or not to follow redirects
$req->followLocation(false); // Default is true
Set connection proxy
$req->setProxy('socks5://127.0.0.1:9050'); // Supported types are http, https, socks4 and socks5
Set connection headers
$req->setHeaders(['HTTP_X_REQUESTED_WITH: XMLHttpRequest', 'Accept-Language: da, en-gb;q=0.8, en;q=0.7']);
Execute the request
$response = $req->exec();
Response
- status (ERROR | OK)
- message
- data
Neat!
httpRequest.class.php on my github https://github.com/psyb0t/httpRequest.class.php