veqryn/curl

PHP 的 Curl 包装器

v2.0.0 2014-12-05 21:55 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:06:05 UTC


README

PHP 的基本 CURL 包装器(有关 PHP 的 libcurl 扩展的更多信息,请参阅 https://php.ac.cn/curl

安装

点击上面的 下载 链接或 git clone git@github.com:veqryn/curl.git

要将此软件包安装到您的项目中,请将以下行添加到您的 composer.json 文件中

"require": {
    "veqryn/curl": "*"
}

用法

初始化

简单要求并初始化 Curl 类,如下所示

require_once('vendor/autoload.php');

use veqryn\Curl\Curl;
use veqryn\Curl\CurlResponse;
use veqryn\Curl\CurlException;

// ...

$curl = new Curl();

执行请求

Curl 对象支持 5 种请求类型:HEAD、GET、POST、PUT 和 DELETE。您必须指定要请求的 URL,并可选地指定要与其一起发送的关联数组或字符串变量。

$response = $curl->head($url, $vars = array());
$response = $curl->get($url, $vars = array()); # The Curl object will append the array of $vars to the $url as a query string
$response = $curl->post($url, $vars = array());
$response = $curl->put($url, $vars = array());
$response = $curl->delete($url, $vars = array());

要使用自定义请求方法,您可以调用 request 方法

$response = $curl->request('YOUR_CUSTOM_REQUEST_TYPE', $url, $vars = array());

所有内置的请求方法,如 putget,都简单封装了 request 方法。例如,post 方法实现如下

public function post($url, $vars = array(), $enctype = null) {
        return $this->request('POST', $url, $vars, $enctype);
    }

示例

$response = $curl->get('google.com?q=test');

# The Curl object will append '&some_variable=some_value' to the url
$response = $curl->get('google.com?q=test', array('some_variable' => 'some_value'));

$response = $curl->post('test.com/posts', array('title' => 'Test', 'body' => 'This is a test'));

所有请求都返回一个 CurlResponse 对象(见下文)或抛出 CurlException(如果发生错误)。您可以使用 $curl->error() 方法访问错误字符串。

CurlResponse 对象

正常的 CURL 请求将在一个响应字符串中返回头和体。此类解析这两个并将它们放入单独的属性中。

例如

$response = $curl->get('google.com');
echo $response->body; # A string containing everything in the response except for the headers
print_r($response->headers); # An associative array containing the response headers

这将显示类似以下内容

<html>
<head>
<title>Google.com</title>
</head>
<body>
Some more html...
</body>
</html>

Array
(
    [Http-Version] => 1.0
    [Status-Code] => 200
    [Status] => 200 OK
    [Cache-Control] => private
    [Content-Type] => text/html; charset=ISO-8859-1
    [Date] => Wed, 07 May 2008 21:43:48 GMT
    [Server] => gws
    [Connection] => close
)

CurlResponse 类定义了魔法方法 __toString(),它将返回响应体,因此 echo $responseecho $response->body 相同

Cookie 会话

默认情况下,cookie 将存储在名为 curl_cookie.txt 的文件中。您可以通过以下方式更改此文件的名称

$curl->cookie_file = 'some_other_filename';

这允许您在请求之间保持会话

基本配置选项

您可以轻松设置 referer 或 user-agent

$curl->referer = 'http://google.com';
$curl->user_agent = 'some user agent string';

如果您愿意,甚至可以手动设置这些头(见下文)

设置自定义头

您可以为请求设置自定义头

$curl->headers['Host'] = 12.345.678.90;
$curl->headers['Some-Custom-Header'] = 'Some Custom Value';

设置自定义 CURL 请求选项

默认情况下,Curl 对象将跟随重定向。您可以通过设置以下内容来禁用此功能

$curl->follow_redirects = false;

您可以为 CURL 请求设置/覆盖许多不同的选项(有关选项的列表,请参阅 curl_setopt 文档

# any of these will work
$curl->options['AUTOREFERER'] = true;
$curl->options['autoreferer'] = true;
$curl->options['CURLOPT_AUTOREFERER'] = true;
$curl->options['curlopt_autoreferer'] = true;

测试

使用 phpunit。只需在 'test' 目录中运行 phpunit。Linux 上的示例(假设 php 在您的路径中)

cd <project_root_dir>/test
../vendor/bin/phpunit

联系

欢迎提出问题、评论和建议:shuber@huberry.com 和/或 VEQRYN [at] hotmail dot com