A minimalistic http/curl request interface that was part of the Semantic MediaWiki code base and is now being deployed as independent library.
This library provides:
HttpRequest
interfaceCurlRequest
as cURL implementation of theHttpRequest
CachedCurlRequest
extendsCurlRequest
to support low-level caching on repeated requestsAsyncCurlRequest
to make use of the cURL multi stack feature
- PHP 5.3 or later
- lib-curl
The recommended installation method for this library is by adding the dependency to your composer.json.
{
"require": {
"onoi/http-request": "~1.0"
}
}
use Onoi\HttpRequest\HttpRequest;
use Onoi\HttpRequest\Exception\BadHttpResponseException;
use Onoi\HttpRequest\Exception\HttpConnectionException;
class Foo {
private $httpRequest = null;
public function __constructor( HttpRequest $httpRequest ) {
$this->httpRequest = $httpRequest;
}
public function makeHttpRequestTo( $url ) {
$this->httpRequest->setOption( CURLOPT_URL, $url );
if ( !$this->httpRequest->ping() ) {
throw new HttpConnectionException( "Couldn't connect" );
}
$this->httpRequest->setOption( CURLOPT_RETURNTRANSFER, true );
$this->httpRequest->setOption( CURLOPT_HTTPHEADER, array(
'Accept: application/x-turtle'
) );
$response = $this->httpRequest->execute();
if ( $this->httpRequest->getLastErrorCode() == 0 ) {
return $response;
}
throw new BadHttpResponseException( $this->httpRequest );
}
}
$httpRequestFactory = new HttpRequestFactory();
$instance = new Foo( $httpRequestFactory->newCurlRequest() );
$instance->makeHttpRequestTo( '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 );
$cachedRequest = $httpRequestFactory->newCachedCurlRequest();
$cachedRequest->setExpiryInSeconds( 60 * 60 );
$instance = new Foo( $cachedRequest );
$instance->makeHttpRequestTo( 'http://example.org' );
If you want to contribute work to the project please subscribe to the developers mailing list and have a look at the contribution guidelinee. A list of people who have made contributions in the past can be found here.
The library provides unit tests that covers the core-functionality normally run by the continues integration platform. Tests can also be executed manually using the PHPUnit configuration file found in the root directory.
- 1.0.0 initial release (2015-07-22)