rakit/curl

另一个 PHP cURL 库

v0.1.0 2016-01-24 11:55 UTC

This package is auto-updated.

Last update: 2024-08-29 04:26:34 UTC


README

这只是另一个 PHP cURL 包装器

安装

如你所见,这个库包含 composer.json 文件。但在这个时候,我还没有将这个库发布到 Packagist。所以,如果你想通过 composer 使用它,你可以在你的 composer.json 文件中手动添加这个仓库。

因此,你的 composer.json 文件应该看起来像这样

{
    "require": {
        "rakit/curl": "dev-master"
    },
    "repositories": [
        {
            "url": "https://github.com/emsifa/rakit-curl",
            "type": "vcs"
        }
    ]
}

然后你可以运行 composer installcomposer update

可选地,你可以下载/克隆这个库并将其加载到你的项目中。

示例

获取请求示例

use Rakit\Curl\Curl;

$request = new Curl('http://wikipedia.com');
$response = $request->get();

// then, you can do something with response object
if(! $response->error()) {

  // getting response file content
  $html = $response->getBody();
  
  // simply get response content type
  $content_type = $response->getContentType(); 
  
  // get content type via curl info
  $content_type = $response->getInfo('content_type'); 
  
  // getting response cookies
  $cookie = $response->getCookie();
  
  // simply get response header item
  $http_version = $response->getHeader('http_version');
  
  // get all header items
  $all_headers = $response->getHeaders();

} else {

  $error_code = $response->getErrno();
  $error_message = $response->getErrorMessage();

}

使用请求参数

use Rakit\Curl\Curl;

$request = new Curl('http://targetsite.com/products');
$request->param('page', 2);
$request->param('category', 'software'); 
$response = $request->get();

// do something with Response object

或者

use Rakit\Curl\Curl;

$params = array(
  'page' => 2,
  'category' => 'software'
);

$request = new Curl('http://targetsite.com/products');
$response = $request->get($params);

// do something with Response object

POST 请求示例

use Rakit\Curl\Curl;

$request = new Curl('http://targetsite.com/register');
$request->param('name', 'John Doe');
$request->param('email', 'johndoe@mail.com');
$request->param('password', '12345');
$response = $request->post();

// do something with Response object

上传文件?使用 addFile 方法

use Rakit\Curl\Curl;

$request = new Curl('http://targetsite.com/register');
$request->param('name', 'John Doe');
$request->param('email', 'johndoe@mail.com');
$request->param('password', '12345');

$request->addFile('avatar', 'path/to/avatar.png');

$response = $request->post();

// do something with Response object

发送一个 cookie

use Rakit\Curl\Curl;

$request = new Curl('http://targetsite.com/products');
$request->cookie('key', 'value');
$response = $request->get();

// do something with Response object

自动重定向

自动重定向允许 cURL 在响应是重定向时(状态码:3xx)自动重定向。

示例

use Rakit\Curl\Curl;

$request = new Curl('http://targetsite.com/admin/product/delete/1');
$request->autoRedirect(5); // 5 is maximum redirection

$response = $request->get();

// what if 6th request is also redirection? it's good to check
if($response->isRedirect()) {
    throw new \Exception("Too many redirection");
}

// do something with response

存储会话

使用 CURLOPT_COOKIEJAR 和 CURLOPT_COOKIEFILE 可以轻松自动存储会话。当你需要登录或进行其他操作以获取需要会话 cookie 的东西时非常有用。为了使用此功能,你必须有一个可以写入文件的目录来存储会话。

例如,你想要获取登录后的重定向页面

use Rakit\Curl\Curl;

$session_file = "path/to/store/sitetarget.session";

$request = new Curl("http://sitetarget.com/login");
$request->autoRedirect(5);
$request->storeSession($session_file);

$response = $request->post(array(
    'username' => 'my_username',
    'password' => 'my_password'
));

// do something with response object

创建简单请求

使用简单请求,你不需要创建新的 cURL 请求对象。但你不能发送 cookie、修改请求头或甚至 cURL 选项。

use Rakit\Curl\Curl;

// simple GET
$response = Curl::doGet('http://targetsite.com', array('page' => 2));

// simple POST 
$response = Curl::doPost('http://targetsite.com/product/delete', array('id' => 5));

响应对象

在上面的示例中,我总是告诉你要处理响应对象,但它是什么?你能对响应对象做什么?

响应对象是从你的 cURL 请求返回的对象。响应对象包含 HTTP 响应的结果数据,例如响应头、cookie、正文等。

以下是你可以对响应对象做的事情

isNotFound()

检查响应状态码是否为 404 的快捷方式。

$response = Curl::doGet("http://sitetarget.com/blablabla");

if($response->isNotFound()) {
   // 404 page not found
}

isRedirect()

如果状态码是 3xx,则返回 true。

// redirecting example

$response = Curl::doGet("http://sitetarget.com/redirect-me");

while($response->isRedirect()) {
    $redirect_url = $response->getHeader("location");
    $response = Curl::doGet($redirect_url);
}

getBody()

获取响应正文,如 html 代码、图像内容等。

length()

获取响应长度(响应正文字符串 + 响应头字符串)。

isHtml()

如果响应内容类型是 text/html,则返回 true。

error()

如果有请求错误,则返回 true。

getErrno()

获取错误代码,0 = 无错误。

getErrorMessage()

获取错误消息,如果没有错误则为空。

getHeader($key, $default = null)

获取头项

$response = Curl::doGet("http://sitetarget.com");

$http_version = $response->getHeader("http_version");
$content_type = $response->getHeader("content-type");
// and many more

getHeaders()

获取所有头项作为数组。

getHeaderString()

获取响应头作为原始头字符串。

getStatusCode()

获取响应状态码。

getContentType()

获取响应内容类型的快捷方式。

getInfo($key, $default = null)

获取类似 curl_getinfo() 的信息项。

getAllInfo()

获取所有信息作为数组。