webiik/curlhttpclient

The CurlHttpClient allows to easily send HTTP request via curl.

1.0 2019-02-28 21:18 UTC

This package is auto-updated.

Last update: 2024-10-29 05:40:40 UTC


README

CurlHttpClient

The CurlHttpClient allows to easily send HTTP request via cURL.

Installation

composer require webiik/curlhttpclient

Example

$chc = new \Webiik\CurlHttpClient\CurlHttpClient();

// Prepare simple GET request (CurlHttpClientReq)
$request = $chc->prepareRequest('https://www.google.com');

// Send request and receive response (CurlHttpClientRes)
$response = $chc->send($req);

Summary

CurlHttpClient

CurlHttpClient prepares cURL requests represented by CurlHttpClientReq, sends cURL requests and receives cURL responses represented by CurlHttpClientRes. It allows you to send asynchronously multiple requests at once.

prepareRequest

prepareRequest(string $url): CurlHttpClientReq

prepareRequest() creates object CurlHttpClientReq which represents set of cURL options.

$request = $chc->prepareRequest('https://www.google.com');

send

send(CurlHttpClientReq $req): CurlHttpClientRes

send() sends cURL request using the options defined in CurlHttpClientReq, returns CurlHttpClientRes.

$response = $chc->send($req);

sendMulti

sendMulti(array $requests): array

sendMulti() sends asynchronously multiple cURL requests at once. Once all requests are completed, it receives array of CurlHttpClientRes.

// Prepare multiple requests
$requests = [
    $chc->prepareRequest('https://www.google.com'),
    $chc->prepareRequest('https://duck.com'),
];

// Send asynchronously multiple requests at once
// Once all requests are completed, get their responses
$responses = $chc->sendMulti($requests);

// Iterate responses
foreach ($responses as $res) {
    /** @var \Webiik\CurlHttpClient\CurlHttpClientRes $res */
}

CurlHttpClientReq

CurlHttpClientReq represents set of cURL options. It provides many methods to easily add most common cURL options.

Connection settings

url

url(string $url): CurlHttpClientReq

url() sets URL to connect to.

$request->url('https://www.google.com');

method

method(string $method): CurlHttpClientReq

method() sets connection method e.g. GET, POST...

$request->method('POST');

port

port(int $port): CurlHttpClientReq

port() sets port to connect to.

$request->port('1212');

followLocation

followLocation(bool $follow, int $maxRedirs = -1, bool $autoReferrer = false): CurlHttpClientReq

followLocation() sets to follow redirects.

$request->followLocation(true);

auth

auth(string $user, string $password, int $authMethod = CURLAUTH_ANY): CurlHttpClientReq

auth() sets authentication credentials.

$request->auth('user', 'password');

proxy

proxy(string $proxyUrl, string $user = '', string $password = '', int $authMethod = CURLAUTH_BASIC): CurlHttpClientReq

proxy() set proxy to connect through.

$request->proxy('socks5://xxx.xxx.xxx.xxx', 'user', 'password');

verifySSL

verifySSL(bool $bool): CurlHttpClientReq

verifySSL() sets to check SSL connection or not.

$request->verifySSL(false);

connectTimeout

connectTimeout(int $sec): CurlHttpClientReq

connectTimeout() sets cURL connection timeout. 0 - wait indefinitely.

$request->connectTimeout(1);

executionTimeout

executionTimeout(int $sec): CurlHttpClientReq

executionTimeout() set cURL transfer timeout. 0 - never quit during transfer.

$request->executionTimeout(1);

lowSpeedLimit

lowSpeedLimit(int $bytes, int $sec): CurlHttpClientReq

lowSpeedLimit() disconnects cURL if it's slower than $bytes/sec for $sec seconds.

// Disconnect when cURL connection speed is lower than 128KB for 10 seconds
$request->lowSpeedLimit(1024 * 128, 10);

Data To Send

userAgent

userAgent(string $agent): CurlHttpClientReq

userAgent() sets user agent HTTP header.

$request->userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15');

referrer

referrer(string $url): CurlHttpClientReq

referrer() sets referrer HTTP header.

$request->referrer('https://www.google.com');

header

header(string $header): CurlHttpClientReq

header() sets HTTP header in format e.g. 'Content-type: image/jpeg'.

$request->header('Content-Type: image/jpeg');

headers

headers(array $headers): CurlHttpClientReq

headers() sets array of HTTP headers in format e.g. ['Content-type: image/jpeg',...]

$request->header([
    'Content-Disposition: attachment; filename="cute-cat.jpg"',
    'Content-Type: image/jpeg',
]);

mimicAjax

mimicAjax(): CurlHttpClientReq

mimicAjax() sets HTTP header to mimic ajax.

$request->mimicAjax();

cookie

cookie(string $name, string $val): CurlHttpClientReq

cookie() sets 'Cookie' HTTP header.

$request->cookie('cat', 'Tom');

cookieFile

cookieFile(string $path): CurlHttpClientReq

cookieFile() sets cookie(s) from file.

$request->cookieFile('cookies.txt');

cookieJar

cookieJar(string $path): CurlHttpClientReq

cookieJar() catches response cookie(s) to file.

$request->cookieJar('cookies.txt');

resetCookie

resetCookie(): CurlHttpClientReq

resetCookie() sets cURL to ignore all previous cookies.

$request->resetCookie();

postData

postData(array $fields, array $curlFiles = []): CurlHttpClientReq

postData() adds post data to cURL request.

$request->postData($_POST, $_FILES);

upload

upload(string $file, int $chunk = 8192): CurlHttpClientReq

upload() sets cURL to upload local file to remote server. $file is local file to be uploaded to remote server.

// Set address to upload to
$request = $chc->prepareRequest('ftp://yourftp.tld');

// Set auth credentials (when required)
$request->auth('user', 'password');

// Set local file to upload
$request->upload('cute-cat.jpg');

// Init uploading
$chc->send($request);

uploadSpeedLimit

uploadSpeedLimit(int $bytesSec): CurlHttpClientReq

uploadSpeedLimit() sets max upload speed in bytes per second.

// Limit upload speed to 1 MB/s
$request->uploadSpeedLimit(1024 * 1024);

Data To Receive

encoding

encoding(string $encoding): CurlHttpClientReq

encoding() sets response encoding. Supported encodings are "identity", "deflate", and "gzip".

$request->encoding('deflate');

receiveBody

receiveBody(bool $bool): CurlHttpClientReq

receiveBody() determines to receive response body or not.

$request->receiveBody(true);

receiveHeaders

receiveHeaders(bool $bool): CurlHttpClientReq

receiveHeaders() determines to receive response headers or not.

$request->receiveHeaders(true);

receiveAsString

receiveAsString(bool $bool): CurlHttpClientReq

receiveAsString() sets cURL to return cURL response as a string instead of outputting it directly.

$request->receiveAsString(true);

downloadToServer

downloadToServer(string $file, int $chunk = 8192): CurlHttpClientReq

downloadToServer() sets cURL to download remote file to local server. $file is used as a storage of remote file.

// Set remote file to download
$request = $chc->prepareRequest('https://domain.tld/cute-cat.jpg');

// Set local file to download to
$request->downloadToServer('cute-cat.jpg');

// Init downloading
$chc->send($request);

downloadToClient

downloadToClient(int $chunk = 8192): CurlHttpClientReq

downloadToClient() sets cURL to stream remote file to client.

// Set remote file to stream
$request = $chc->prepareRequest('https://domain.tld/cute-cat.jpg');

// Tell cURL to stream remote file to client
$request->downloadToClient();

// Set appropriate headers
header('Content-Disposition: attachment; filename="cute-cat.jpg"');
header('Content-Type: image/jpeg');

// Init streaming
$chc->send($request);

downloadSpeedLimit

downloadSpeedLimit(int $bytesSec): CurlHttpClientReq

downloadSpeedLimit() sets max download speed in bytes per second.

// Limit download speed to 1 MB/s
$request->downloadSpeedLimit(1024 * 1024);

Custom

verbose

verbose(bool $bool): CurlHttpClientReq

verbose() sets cURL to receive verbose response info.

$request->verbose(true);

curlOption

curlOption(int $option, $val): CurlHttpClientReq

curlOption() sets a cURL option.

$request->curlOption(CURLOPT_TCP_NODELAY, 1);

curlOptions

curlOptions(array $options): CurlHttpClientReq

curlOptions() sets an array of cURL options.

$request->curlOptions([
	CURLOPT_TCP_NODELAY => 1,
	CURLOPT_FORBID_REUSE => 1,
]);

progressFile

progressFile(string $uniqueName, string $dir): CurlHttpClientReq

progressFile() sets a file to store download/upload progress to. Progress is stored in JSON format.

$request->progressFile('fu37icnj', __DIR__);

progressJson

progressJson(): CurlHttpClientReq

progressJson() sets cURL to print upload/download progress as a JSON (without content-type header).

$request->progressJson();

CurlHttpClientRes

CurlHttpClientRes represents cURL response. It provides methods to easily access most common response informations.

header

header(string $name, bool $sensitive = true, bool $raw = false)

header() gets response header by header name.

Parameters

  • name header name
  • sensitive determines if header name is case sensitive
  • raw determines to return only header value(false) or complete header(true)
$header = $response->header('Content-Type');

headers

headers(): array

headers() gets array of all response headers.

$headers = $response->headers();

cookie

cookie(string $name, bool $sensitive = true, bool $raw = false)

cookie() gets response cookie value by cookie name.

Parameters

  • name cookie name
  • sensitive determines if cookie name is case sensitive
  • raw determines to return only cookie value(false) or complete cookie header(true)
$cookie = $response->cookie('cat');

cookies

cookies(): array

cookies() gets array of all response cookie headers.

$cookies = $response->cookies();

cookiesAssoc

cookiesAssoc(): array

cookiesAssoc() gets associative array of all response cookies.

$cookies = $response->cookiesAssoc();

body

body(): string

body() gets response body.

$body = $response->body();

size

size(): int

size() gets response size in bytes.

$size = $response->size();

mime

mime(): string

mime() gets response content type.

$mime = $response->mime();

statusCode

statusCode(): int

statusCode() gets response HTTP status code.

$httpStatusCode = $response->statusCode();
if ($httpStatusCode == 200) {
    // Remote page responded with HTTP status OK
}

errMessage

errMessage(): string

errMessage() gets cURL error message.

$curlErrMessage = $response->errMessage();

errNum

errNum(): int

errNum() gets cURL error number.

$curlErrNum = $response->errNum();
if (!$curlErrNum) {
    // Curl request is OK
}

isOk

isOk(): bool

isOk() indicates if cURL request was ok.

if ($response->isOk()) {
    // Curl request is OK
}

info

info(): array

info() gets cURL info array.

$curlInfo = $response->info();

requestHeaders

requestHeaders(): array

requestHeaders() gets array of all request headers.

$requestHeaders = $response->requestHeaders();

requestCookies

requestCookies(): array

requestCookies() gets array of all request cookie headers.

$requestCookies = $response->requestCookies();

Resources