PHP code example of chillerlan / php-httpinterface
1. Go to this page and download the library: Download chillerlan/php-httpinterface 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/ */
chillerlan / php-httpinterface example snippets
$options = new HTTPOptions;
$options->ca_info = '/path/to/cacert.pem';
$options->user_agent = 'my cool user agent 1.0';
$options->dns_over_https = 'https://cloudflare-dns.com/dns-query';
$httpClient = new CurlClient($responseFactory, $options, $logger);
$request = $requestFactory->createRequest('GET', 'https://www.example.com?foo=bar');
$httpClient->sendRequest($request);
$handler = new class () implements MultiResponseHandlerInterface{
public function handleResponse(
ResponseInterface $response, // the incoming response
RequestInterface $request, // the corresponding request
int $id, // the request id
array|null $curl_info, // the curl_getinfo() result for this request
):RequestInterface|null{
if($response->getStatusCode() !== 200){
// return the failed request back to the stack
return $request;
}
try{
$body = $response->getBody();
// the response body is empty for some reason, we pretend that's fine and exit
if($body->getSize() === 0){
return null;
}
// parse the response body, store the result etc.
$data = $body->getContents();
// save data to file, database or whatever...
// ...
}
catch(Throwable){
// something went wrong, return the request to the stack for another try
return $request;
}
// everything ok, nothing to return
return null;
}
};
$options = new HTTPOptions;
$options->ca_info = '/path/to/cacert.pem';
$options->user_agent = 'my cool user agent 1.0';
$options->sleep = 750000; // microseconds, see usleep()
$options->window_size = 5;
$options->retries = 1;
$multiClient = new CurlMultiClient($handler, $responseFactory, $options, $logger);
// create and add the requests
foreach(['..', '...', '....'] as $item){
$multiClient->addRequest($factory->createRequest('GET', $endpoint.'/'.$item));
}
// process the queue
$multiClient->process();
$options = new HTTPOptions;
$options->user_agent = 'my cool user agent 1.0';
$options->ssl_verifypeer = false;
$options->curl_options = [
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_MAXREDIRS => 25,
];
$httpClient = new CurlClient($responseFactory, $options, $logger);
$urlExtractor = new URLExtractor($httpClient, $responseFactory);
$request = $factory->createRequest('GET', 'https://t.co/ZSS6nVOcVp');
$urlExtractor->sendRequest($request); // -> response from the final location
// you can retrieve an array with all followed locations afterwards
$responses = $this->http->getResponses(); // -> ResponseInterface[]
// if you just want the URL of the final location, you can use the extract method:
$url = $this->http->extract('https://t.co/ZSS6nVOcVp'); // -> https://api.guildwars2.com/v2/build
$loggingClient = new LoggingClient($httpClient, $logger);
$loggingClient->sendRequest($request); // -> log to output given via logger