rjr/curl

PHP 的 RuanJiaran-CURL 类

v0.2 2018-11-01 05:45 UTC

This package is auto-updated.

Last update: 2024-09-29 05:08:34 UTC


README

此库提供了 PHP cURL 扩展的对象封装。

如果您在安装或使用过程中有任何问题或问题,请 创建一个问题

安装

要使用 composer 安装此库,请在控制台中运行以下命令

composer require rjr/curl

或者在 composer.json 文件的 require 部分手动添加此包

"rjr/curl": "^1.5"

使用示例

$curl = new Rjr\Curl();
$curl->get('http://www.example.com/');
$curl = new Rjr\Curl();
$curl->get('http://www.example.com/search', array(
    'q' => 'keyword',
));
$curl = new Rjr\Curl();
$curl->post('http://www.example.com/login/', array(
    'username' => 'myusername',
    'password' => 'mypassword',
));
$curl = new Rjr\Curl();
$curl->setBasicAuthentication('username', 'password');
$curl->setUserAgent('');
$curl->setReferrer('');
$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);
$curl = new Rjr\Curl();
$curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
$curl->get('https://encrypted.example.com/');
$curl = new Rjr\Curl();
$curl->put('http://api.example.com/user/', array(
    'first_name' => 'Zach',
    'last_name' => 'Borboa',
));
$curl = new Rjr\Curl();
$curl->patch('http://api.example.com/profile/', array(
    'image' => '@path/to/file.jpg',
));
$curl = new Rjr\Curl();
$curl->delete('http://api.example.com/user/', array(
    'id' => '1234',
));
$curl->close();
// 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
$curl = new Rjr\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);

## 测试

为了测试此库

  1. 创建一个分支
  2. 将分支克隆到您的机器上
  3. 安装依赖项 composer install
  4. 运行单元测试 ./vendor/bin/phpunit tests