curl OOP Wrapper


Latest Version Software License GitHub Tests Action Status Quality Score Code Coverage Type Coverage Total Downloads

Simple PHP Curl OOP wrapper for efficient request.

For a more complex or abstracted curl wrapper, use Guzzle.


Via Packagist

$ composer require piedweb/curl


Quick Example :

$url = 'https://piedweb.com';
$request = new \PiedWeb\Curl\ExtendedCliend($url);
    ->setDownloadOnlyIf('PiedWeb\Curl\Helper::checkContentType') // 'PiedWeb\Curl\Helper::checkStatusCode'
$result = $request->request();
if ($result instanceof \PiedWeb\Curl\Response) {
    $content = $this->getContent();

Static Wrapper Methods :

use PiedWeb\Curl\StaticClient as Client;

Client::request($url); // @return ?string
Client::get(); // @return PiedWeb\Curl\ExtendedClient

All Other Methods :

$r = new PiedWeb\Curl\ExtendedClient(?string $url);
    ->setOpt(CURLOPT_*, mixed 'value')

	// Preselect Options to avoid eternity wait
    ->setDefaultGetOptions($connectTimeOut = 5, $timeOut = 10, $dnsCacheTimeOut = 600, $followLocation = true, $maxRedirs = 5)
    ->setDefaultSpeedOptions() // no header except if setted, 1 redir max, no ssl check
    ->setCookie(string $cookie)
    ->setReferer(string $url)
    ->fakeBrowserHeader(bool $doIt = true)
    ->setUserAgent(string $ua)
        ->getUserAgent() // @return string

    ->setDownloadOnlyIf(callable $func) // @param $ContentType can be a String or an Array
    ->setMaximumResponseSize(int $tooBig = 200000) // @defaut 2Mo
    ->setDownloadOnly($range = '0-500')

    ->setPost(array $post)


    ->setProxy(string '[scheme]proxy-host:port[:username:passwrd]') // Scheme, username and passwrd are facultatives. Default Scheme is http://


    $r->request(); // @return true if request succeed else false (see getError)

$response = $r->getResponse(); // @return PiedWeb\Curl\Response or int corresponding to the curl error

$response->getUrl(); // @return string
$response->getContentType(); // @return string
$response->getContent(); // @return string
$response->getHeaders($returnArray = true); // @return array Response Header (or in a string if $returnArray is set to false)
$response->getCookies(); // @return string
$response->getUrl(); // @return string

$response->getError(); // Equivalent to curl function curl_errno
$response->getErrorMessage(); // .. curl_error

use PiedWeb\Curl\ResponseFromCache;

$response = new ResponseFromCache(  // same methods than Response except getRequest return null
    string $filePathOrContent,
    ?string $url = null,
    array $info = [],
    $headers = PHP_EOL.PHP_EOL


Please see contributing



The MIT License (MIT). Please see License File for more information.

Open Source Package