1. Go to this page and download the library: Download byjg/webrequest 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/ */
$uri = \ByJG\Util\Uri::getInstanceFromString('http://www.example.com/page');
// Define the contents to upload using a list of MultiPartItem objects
$uploadFile = [];
$uploadFile[] = new \ByJG\WebRequest\MultiPartItem('field1', 'value1');
$uploadFile[] = new \ByJG\WebRequest\MultiPartItem(
'field2',
'{"key": "value2"}',
'filename.json',
'application/json; charset=UTF-8'
);
$uploadFile[] = new \ByJG\WebRequest\MultiPartItem('field3', 'value3');
// Use the Wrapper to create the Request
$request = \ByJG\WebRequest\Helper\RequestMultiPart::build(Uri::getInstanceFromString($uri),
"POST",
$uploadFile
);
// Do the request as usual
$response = \ByJG\WebRequest\HttpClient::getInstance()->sendRequest($request);
$client = \ByJG\WebRequest\HttpClient::getInstance()
->withNoFollowRedirect() // HttpClient will not follow redirects (status codes 301 and 302). Default is follow
->withNoSSLVerification() // HttpClient will not validate the SSL certificate. Default is validate.
->withProxy($uri) // Define a http Proxy based on the URI.
->withCurlOption($key, $value) // Set an arbitrary CURL option (use with caution)
;
// Create the instances of the lient::getInstance();
$onSucess = function ($response, $id) {
// Do something with Response object
};
$onError = function ($error, $id) use (&$fail) {
// Do something
};
// Create the HttpClientParallel
$multi = new \ByJG\WebRequest\HttpClientParallel(
$httpClient,
$onSucess,
$onError
);
// Add the request to run in parallel
$request1 = Request::getInstance($uri1);
$request2 = Request::getInstance($uri2);
$request3 = Request::getInstance($uri3);
$multi
->addRequest($request1)
->addRequest($request2)
->addRequest($request3);
// Start execute and wait to finish
// The results will be get from the closure defined above.
$multi->execute();
$expectedResponse = new Response(200);
$mock = $this->object = new MockClient($expectedResponse);
$response = $mock->sendRequest(new Request("http://example.com"));
assertEquals($expectedResponse, $response);