aicial / webrequest
Web request library providing GET PUT POST HEAD Cookies and more for file_get_contents
Requires
- php: >=5.3.0
 
Requires (Dev)
- phpunit/phpunit: 4.0.*
 
This package is not auto-updated.
Last update: 2020-07-30 16:02:30 UTC
README
Provides a simple way to interact with web services (currently using file_get_contents()). Easily tracks cookies exchanged - making for easier scraping. (See Thirdlane example below)
Installation
cd <project_dir>
composer require aicial\webrequest
Usage
require __DIR__ . '/vendor/autoload.php';	//Use composers autoload
$webRequest = new Aicial\Webrequest\Webrequest();
$webRequest->setURL('https://YOUR_SERVICE.TLD/');
$webRequest->setJSON(true);
$serverResponse = $webRequest->go();
var_dump($serverResponse);
Methods
go()			//Perform the web request
setCookies()	//Set any cookies to be included in the headers
setHeaders()	//Set any headers to be sent to the target URL
setJSON()		//Is this transaction sending / expecting JSON
setJSONPretty()	//Should JSON being sent be encoded in an easily readable format?
setMethod()		//Set the HTTP method GET, HEAD, PUT, POST are valid
setPost()		//Set post contents
setQuerystring()	//Set querystring data
setTrackCookies()	//Should cookies be tracked?
setURL()		//Set the target URL Must include http:// or https://
setVerifyPeer()   //Should SSL peers be verified?
setWebTimeout()   //Set's the value that determines how long we wait for a response
Example - Using cookies
Tracking exchanged cookies is handled within the package. You just need to turn it on to have a simple transaction with a server to be scraped. This is an example of getting a list of recorded calls from a ThirdLane PBX server
<?php
require __DIR__ . '/vendor/autoload.php';	//Use composers autoload
$webrequest = new Aicial\Webrequest\Webrequest();
$url = 'http://mypbx.server:10000';
$user = 'mytlusername';
$password = 'mytlpassword';
$loginUrl = 'session_login.cgi';
$recordedUrl = 'asterisk/recorded_calls.cgi';
$webrequest->setURL($url.'/'.$loginUrl);
$webrequest->setVerifyPeer(false);		//Most Thirdlane servers have invalid SSL
$webrequest->setTrackCookies(true);	//Return cookies received
$response = $webrequest->go();			//Make the initial GET request
$post = array('user' => $this->config['username'], 'pass' => $this->config['password'], 'page' => '/', 'save' => '1');
$webrequest->setMethod('POST');	//Use a form POST
$webrequest->setPost($post);	//Supply the POST data
$response = $webrequest->go();	//Notice the URL has not been set again - it is still set from before.
$webrequest->setURL($url.'/'.$recordedUrl);	//The URL for Recorded Calls Listing
$webrequest->setMethod('GET');		//Back to using HTTP GET
$webrequest->setPost();			//Clear the POST array
$response = $webrequest->go();	//Get the page data
print_r($response);	//Do what you want with the content
Notes
Whilst the package supports cookies - it does not honor domain or expiry (at this time).
The sending server can not expire out (delete) a cookie. Some interactions with servers require this for privilege escalation. If this is the case for you - the only work around for this at the moment is to know the moment in time you need to delete the cookie, and do it in code.
History
4 Feb 2016 - Initial public release, example code added
5 Feb 2016 - Fixed missing ability to set the HTTP timeout, added notes about limitations
Copyright
2016 Troy Kelly, Aicial Pty Ltd (Australia)