shuber/curl

PHP的Curl包装器

维护者

详细信息

github.com/hamstar/curl

源代码

安装次数: 997,377

依赖: 11

建议者: 0

安全性: 0

星标: 31

关注者: 3

分支: 179

v1.0.0 2012-06-02 20:57 UTC

This package is not auto-updated.

Last update: 2024-09-22 02:58:55 UTC


README

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

安装

点击上面的下载链接或使用git clone git://github.com/shuber/curl.git

使用方法

初始化

简单地像这样引入和初始化Curl

require_once 'curl.php';
$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方法的实现如下

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

示例

$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对象(见下文)或如果发生错误则返回false。您可以使用$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';

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

基本配置选项

您可以轻松设置引用或用户代理

$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;

测试

使用ztest,只需将其下载到path/to/curl/test/ztest(或您的php include_path中的任何其他位置)

然后运行test/runner.php

联系方式

欢迎提出问题、评论和建议:shuber@huberry.com