PHP code example of webiik / curlhttpclient
1. Go to this page and download the library: Download webiik/curlhttpclient 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/ */
webiik / curlhttpclient example snippets
$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);
prepareRequest(string $url): CurlHttpClientReq
$request = $chc->prepareRequest('https://www.google.com');
send(CurlHttpClientReq $req): CurlHttpClientRes
$response = $chc->send($req);
sendMulti(array $requests): array
// 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 */
}
url(string $url): CurlHttpClientReq
$request->url('https://www.google.com');
method(string $method): CurlHttpClientReq
$request->method('POST');
port(int $port): CurlHttpClientReq
$request->port('1212');
followLocation(bool $follow, int $maxRedirs = -1, bool $autoReferrer = false): CurlHttpClientReq
$request->followLocation(true);
auth(string $user, string $password, int $authMethod = CURLAUTH_ANY): CurlHttpClientReq
$request->auth('user', 'password');
proxy(string $proxyUrl, string $user = '', string $password = '', int $authMethod = CURLAUTH_BASIC): CurlHttpClientReq
$request->proxy('socks5://xxx.xxx.xxx.xxx', 'user', 'password');
verifySSL(bool $bool): CurlHttpClientReq
$request->verifySSL(false);
connectTimeout(int $sec): CurlHttpClientReq
$request->connectTimeout(1);
executionTimeout(int $sec): CurlHttpClientReq
$request->executionTimeout(1);
lowSpeedLimit(int $bytes, int $sec): CurlHttpClientReq
// Disconnect when cURL connection speed is lower than 128KB for 10 seconds
$request->lowSpeedLimit(1024 * 128, 10);
userAgent(string $agent): CurlHttpClientReq
$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(string $url): CurlHttpClientReq
$request->referrer('https://www.google.com');
header(string $header): CurlHttpClientReq
$request->header('Content-Type: image/jpeg');
headers(array $headers): CurlHttpClientReq
$request->header([
'Content-Disposition: attachment; filename="cute-cat.jpg"',
'Content-Type: image/jpeg',
]);
mimicAjax(): CurlHttpClientReq
$request->mimicAjax();
cookie(string $name, string $val): CurlHttpClientReq
$request->cookie('cat', 'Tom');
cookieFile(string $path): CurlHttpClientReq
$request->cookieFile('cookies.txt');
cookieJar(string $path): CurlHttpClientReq
$request->cookieJar('cookies.txt');
resetCookie(): CurlHttpClientReq
$request->resetCookie();
postData(array $fields, array $curlFiles = []): CurlHttpClientReq
$request->postData($_POST, $_FILES);
upload(string $file, int $chunk = 8192): CurlHttpClientReq
// Set address to upload to
$request = $chc->prepareRequest('ftp://yourftp.tld');
// Set auth credentials (when ading
$chc->send($request);
uploadSpeedLimit(int $bytesSec): CurlHttpClientReq
// Limit upload speed to 1 MB/s
$request->uploadSpeedLimit(1024 * 1024);
encoding(string $encoding): CurlHttpClientReq
$request->encoding('deflate');
receiveBody(bool $bool): CurlHttpClientReq
$request->receiveBody(true);
receiveHeaders(bool $bool): CurlHttpClientReq
$request->receiveHeaders(true);
receiveAsString(bool $bool): CurlHttpClientReq
$request->receiveAsString(true);
downloadToServer(string $file, int $chunk = 8192): CurlHttpClientReq
// 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(int $chunk = 8192): CurlHttpClientReq
// 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(int $bytesSec): CurlHttpClientReq
// Limit download speed to 1 MB/s
$request->downloadSpeedLimit(1024 * 1024);
verbose(bool $bool): CurlHttpClientReq
$request->verbose(true);
curlOption(int $option, $val): CurlHttpClientReq
$request->curlOption(CURLOPT_TCP_NODELAY, 1);
curlOptions(array $options): CurlHttpClientReq
$request->curlOptions([
CURLOPT_TCP_NODELAY => 1,
CURLOPT_FORBID_REUSE => 1,
]);
progressFile(string $uniqueName, string $dir): CurlHttpClientReq
$request->progressFile('fu37icnj', __DIR__);
progressJson(): CurlHttpClientReq
$request->progressJson();
header(string $name, bool $sensitive = true, bool $raw = false)
$header = $response->header('Content-Type');
headers(): array
$headers = $response->headers();
cookie(string $name, bool $sensitive = true, bool $raw = false)
$cookie = $response->cookie('cat');
cookies(): array
$cookies = $response->cookies();
cookiesAssoc(): array
$cookies = $response->cookiesAssoc();
body(): string
$body = $response->body();
size(): int
$size = $response->size();
mime(): string
$mime = $response->mime();
statusCode(): int
$httpStatusCode = $response->statusCode();
if ($httpStatusCode == 200) {
// Remote page responded with HTTP status OK
}
errMessage(): string
$curlErrMessage = $response->errMessage();
errNum(): int
$curlErrNum = $response->errNum();
if (!$curlErrNum) {
// Curl request is OK
}
isOk(): bool
if ($response->isOk()) {
// Curl request is OK
}
info(): array
php
$curlInfo = $response->info();
php
requestHeaders(): array
php
$requestHeaders = $response->requestHeaders();
php
requestCookies(): array
php
$requestCookies = $response->requestCookies();