ledc/curl

基于Curl的请求类库

v8.1.0 2024-03-29 00:40 UTC

This package is auto-updated.

Last update: 2024-03-29 00:41:20 UTC


README

This library provides an object-oriented and dependency free wrapper of the PHP cURL extension.

Maintainability Test Coverage Total Downloads Tests

If you have questions or problems with installation or usage create an Issue.

Installation

In order to install this library via composer run the following command in the console:

composer require ledc/curl

Usage examples

A few example for using CURL with get:

use Ledc\Curl\Curl;

$curl = (new Curl())->get('http://www.example.com/');
if ($curl->isSuccess()) {
    // do something with response
    var_dump($curl->response);
}
// ensure to close the curl connection
$curl->close();

upload file metadata

use Ledc\Curl\Curl;

$curl = new Curl();
$json = '[{"length":7533293969,"path":["Brotherhood.of.the.Wolf.2001.BluRay.1080p.x265.10bit.2Audio.mkv"]},{"length":958431,"path":["cover.jpg"]},{"length":760,"path":["Brotherhood.of.the.Wolf.2001.BluRay.1080p.x265.10bit.2Audio.nfo"]}]';
$curl->addFile('file', '1.json', $json, 'application/json');
$curl->upload('http://www.example.com/upload');
$response = $curl->response;
printf($response);

Or with params, values will be encoded with PHP_QUERY_RFC1738:

use Ledc\Curl\Curl;
$curl = (new Curl())->get('http://www.example.com/search', [
    'q' => 'keyword',
]);

An example using post

use Ledc\Curl\Curl;
$curl = new Curl();
$curl->post('http://www.example.com/login/', [
    'username' => 'myusername',
    'password' => 'mypassword',
]);

An exampling using basic authentication, remove default user agent and working with error handling

use Ledc\Curl\Curl;
$curl = new Curl();
$curl->setBasicAuthentication('username', 'password');
$curl->setUserAgent('');
$curl->setHeader('X-Requested-With', 'XMLHttpRequest');
$curl->setCookie('key', 'value');
$curl->get('http://www.example.com/');

if ($curl->error) {
    echo $curl->error_code;
} else {
    echo $curl->response;
}

var_dump($curl->request_headers);
var_dump($curl->response_headers);

SSL verification setup:

use Ledc\Curl\Curl;
$curl = new Curl();
$curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
$curl->get('https://encrypted.example.com/');

Example access to curl object:

curl_set_opt($curl->curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
curl_close($curl->curl);

Example of downloading a file or any other content

use Ledc\Curl\Curl;
$curl = new Curl();
// open the file where the request response should be written
$file_handle = fopen($target_file, 'w+');
// pass it to the curl resource
$curl->setOpt(CURLOPT_FILE, $file_handle);
// do any type of request
$curl->get('https://github.com');
// disable writing to file
$curl->setOpt(CURLOPT_FILE, null);
// close the file for writing
fclose($file_handle);