Web Request

Build Status SensioLabsInsight

A lightweight and highly customized CURL wrapper for making RESt calls and a wrapper for call dynamically SOAP requests. Just one class and no dependencies.

Basic Usage

$webRequest = new WebRequest('http://www.example.com/page');
$result = $webRequest->get();
//$result = $webRequest->post();
//$result = $webRequest->delete();
//$result = $webRequest->put();

Passing arguments

$webRequest = new WebRequest('http://www.example.com/page');
$result = $webRequest->get(['param'=>'value']);
//$result = $webRequest->post(['param'=>'value']);
//$result = $webRequest->delete(['param'=>'value']);
//$result = $webRequest->put(['param'=>'value']);

Passing a string payload (JSON)

$webRequest = new WebRequest('http://www.example.com/page');
$result = $webRequest->postPayload('{teste: "value"}', 'application/json');
//$result = $webRequest->putPayload('{teste: "value"}', 'application/json');
//$result = $webRequest->deletePayload('{teste: "value"}', 'application/json');


$webRequest = new WebRequest('http://www.example.com/page');
$webRequest->setCurlOption(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
$result = $webRequest->get();

Upload a file using “multipart/form-data”

$webRequest = new WebRequest('http://www.example.com/page');

// Define the Upload File
$upload = [];
$upload[] = new MultiPartItem('fieldName', 'fieldContent');
$upload[] = new MultiPartItem('fieldName', 'fieldContent', 'mime-filename.ext');

// Post and get the result
$result = $webRequest->postMultiPartForm($upload);

Calling Soap Classes

$webRequest = new WebRequest('http://www.example.com/soap');
$resutl = $webRequest->soapCall('soapMethod', ['arg1' => 'value']);


You can use the WebRequest to do several differents requests in parallel.

To use this funcionallity you need:

  1. Create a instance of the WebRequestMulti class
  2. Add the WebRequest instance
  3. Execute

See a basic example to execute the WebRequest and does not care about the result:

$webRequestMulti = new WebRequestMulti();

        new \ByJG\Util\WebRequest('http://localhost/api/myrest'),
        new \ByJG\Util\WebRequest('http://anotherserver/method'),
            'param' => 'somevalue',
            'anotherparam' => '30130000'


You can optionally create a \Closure function for process the successfull and the error result.

For example:

$onSuccess = function ($body, $id) {
    echo "[$id] => $body\n";

$onError = function ($error, $id) {
    throw new \Exception("$error on id '$id'");

And then pass to WebRequestMulti constructor:

$webRequestMulti = new WebRequestMulti($onSuccess, $onError);

or pass to the addRequestMethod:



composer install "byjg/webrequest=1.0.*"

Running Tests

Starting the server

cd tests
php -S localhost:8080 -t tests/server & 

Note: It is more assertive create a webserver with the server folder instead to use the PHP built-in webserver.

Running the integration tests


Stopping the server

killall -9 php