Usage - ossbrownie/http-client GitHub Wiki
Creation HTTPClient.
$httpClient = new HttpClient(
new CurlAdapter(new CurlAdaptee())
);
Creating and configuring custom HTTP headers.
$header1 = new Header();
$header1
->setName('test1')
->setValue('Simple1');
$header2 = new Header(array(
'name' => 'test3',
'value' => 'Simple3'
));
Creating and configuring custom cookies.
$cookie1 = new Cookie();
$cookie1
->setName('cookieName1')
->setValue('cookieValue1');
$cookie2 = new Cookie(array(
'name' => 'cookieName2',
'value' => 'cookieValue2',
));
Assembling the request.
$request = new Request();
$request
/* Sets the query method. */
->setMethod(Request::HTTP_METHOD_GET)
/* Sets the request URL. */
->setUrl('https://api.github.com/emojis')
/* Sets the body of the request. */
->setBody('{"Hello":"World"}')
/* Sets the query parameters. (With POST or PUT queries redefine the body of the request.) */
->addParam('page', '5555')
/* The format of the transmitted data. (Ignored by POST or PUT requests) */
->setBodyFormat(Request::FORMAT_APPLICATION_JSON)
/* Response timeout in seconds. */
->setTimeOut(100)
/* Disabling validation of certificates for HTTPS requests. */
->disableSSLValidation()
/* Sets the HTTP authentication data. */
->setAuthentication('tester', '123')
/* Adds the HTTP headers to the request. */
->addHeader($header1)
->addHeader($header2)
/* Adds the cookies to the request. */
->addCookie($cookie1)
->addCookie($cookie2);
Execution of the HTTP request.
$response = $httpClient->request($request);
Example response:
/* Gets the HTTP response code. */
$httpCode = $response->getHttpCode();
/* Gets the HTTP response header. */
$contentType = $response->getHttpHeaderList()->get('Content-Type');
if (!empty($contentType)) {
$contentTypeValue = $contentType->getValue();
}
/* Gets the response cookie. */
$cookieTest3 = $response->getHttpCookieList()->get('test3');
if (!empty($cookieTest3)) {
$cookieTest3Value = $cookieTest3->getValue();
}
/* Gets the execution time of the query. */
$requestRuntime = $response->getRuntime();
/* Gets the response body. */
$responseBody = $response->getBody();