alioygur / curl
PHP的基本CURL包装器
v1.0
2014-11-05 15:05 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 4.3.*
This package is not auto-updated.
Last update: 2024-09-24 08:12:27 UTC
README
PHP的基本CURL包装器(有关PHP的libcurl扩展的更多信息,请参阅https://php.ac.cn/curl)
安装
此库可通过Composer获取
{ "require": { "alioygur/curl": "~1.0" } }
用法
简单用法示例
初始化并使用Curl
类,如下所示
<?php use Alioygur\Curl\Curl; $curl = new Curl(); $response = $curl ->setOption('CURLOPT_FOLLOW_REDIRECTS', false) ->setHeader('User-Agent', 'My Name is Heisenberg!') ->get('http://example.com');
执行请求
Curl对象支持5种请求类型:HEAD、GET、POST、PUT和DELETE。您必须指定请求的URL,并可选地指定一个关联数组或字符串形式的变量与它一起发送。
$response = $curl->head($url, $vars = []); $response = $curl->get($url, $vars = []); # The Curl object will append the array of $vars to the $url as a query string $response = $curl->post($url, $vars = []); $response = $curl->put($url, $vars = []); $response = $curl->delete($url, $vars = []);
要使用自定义请求方法,您可以调用request
方法
$response = $curl->request('YOUR_CUSTOM_REQUEST_TYPE', $url, $vars = []);
所有内置请求方法,如put
和get
,只是简单地封装了request
方法。例如,post
方法的实现如下
function post($url, $vars = []) { 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 Headers ------------------------------------------------------------------------- # Get the response body echo $response->body(); # A string containing everything in the response except for the headers # Get the response headers print_r($response->headers()); # An associative array containing the response headers # Pick one from response headers echo $response->headers('Content-Type'); # text/html # You can also use those methods $response->status(); # 200 OK $response->statusCode(); # 200 $response->ContentType(); # text/html # Request Headers -------------------------------------------------------------------------- # Get the request headers $response->requestHeaders(); # An associative array containing the request headers # Pick one from request headers echo $response->requestHeaders('Version'); # HTTP/1.1 # Curl Information ------------------------------------------------------------------------- Get information regarding a specific transfer. See, http://php.ac.cn/manual/en/function.curl-getinfo.php # Get all $response->getInfo(); # An associative array containing the curl information # Pick one $response->getInfo('total_time'); # 0.14257
CurlResponse类定义了__toString()魔法方法,它将返回响应体,因此echo $response
与echo $response->body
相同
Cookie会话
默认情况下,cookie将被存储在名为curl_cookie.txt
的文件中。您可以通过以下方式更改此文件的名称
$curl->setCookieFile('some_other_filename');
这允许您在请求之间保持会话
设置自定义头
您可以设置要随请求发送的自定义头
$curl->setheader('SOME_KEY', 'some value'); # you can also method chaining $response = $curl->setHeader('Content-Type', 'application/json') ->setHeader('User-Agent', 'Mozilla/5.0 (X11; Linux...') ->get('http://example.com');
设置自定义CURL请求选项
默认情况下,Curl对象将跟随重定向。您可以禁用此设置
$curl->setOptions('CURLOPT_FOLLOW_REDIRECTS', false);
您可以设置/覆盖CURL请求的许多不同选项(有关更多信息,请参阅curl_setopt()文档)
身份验证
设置HTTP基本认证方法的用户名和密码。
$curl->setAuth('username', 'password');
联系方式
欢迎提出问题、评论和建议:alioygur@gmail.com