1. Go to this page and download the library: Download onoi/http-request 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/ */
onoi / http-request example snippets
use Onoi\HttpRequest\CurlRequest;
use Onoi\HttpRequest\Exception\BadHttpResponseException;
use Onoi\HttpRequest\Exception\HttpConnectionException;
class Foo {
private $curlRequest = null;
public function __constructor( CurlRequest $curlRequest ) {
$this->curlRequest = $curlRequest;
}
public function doMakeHttpRequestTo( $url ) {
$this->curlRequest->setOption( CURLOPT_URL, $url );
if ( !$this->curlRequest->ping() ) {
throw new HttpConnectionException( "Couldn't connect" );
}
$this->curlRequest->setOption( CURLOPT_RETURNTRANSFER, true );
$this->curlRequest->setOption( CURLOPT_HTTPHEADER, array(
'Accept: application/x-turtle'
) );
$response = $this->curlRequest->execute();
if ( $this->curlRequest->getLastErrorCode() == 0 ) {
return $response;
}
throw new BadHttpResponseException( $this->curlRequest );
}
}
$httpRequestFactory = new HttpRequestFactory();
$instance = new Foo( $httpRequestFactory->newCurlRequest() );
$response = $instance->doMakeHttpRequestTo( 'http://example.org' );
OR
$cacheFactory = new CacheFactory();
$compositeCache = $cacheFactory->newCompositeCache( array(
$cacheFactory->newFixedInMemoryLruCache( 500 ),
$cacheFactory->newDoctrineCache( new \Doctrine\Common\Cache\RedisCache() )
) );
$httpRequestFactory = new HttpRequestFactory( $compositeCache );
$cachedCurlRequest = $httpRequestFactory->newCachedCurlRequest();
// Responses for a request with the same signature (== same endpoint and same query
// content) will be cached if the request was successful for a specified 1 h (3600 sec)
$cachedCurlRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, 60 * 60 );
$instance = new Foo( $cachedCurlRequest );
$response = $instance->doMakeHttpRequestTo( 'http://example.org' );
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.