wykleph/curl

PHP 中简单的同步和异步 cURL 请求。

v0.11 2016-02-03 17:21 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:09:49 UTC


README

A fluent API wrapper for libcurl in php. Setting options and headers is done using method chaining instead of setting options explicitly using the libcurl constants.

使用 Curler 类在 PHP 中调试 cURL 命令也非常简单。只需将 dryRun() 方法链接到方法链的末尾而不是 go() 方法,它将在不发出请求的情况下输出所有 cURL 请求信息。

有关 libcurl 的信息,请参阅 https://php.ac.cn/manual/en/book.curl.php

注意:此库处于初级阶段。

完整文档即将推出。

入门

使用 Composer 安装

wykleph/curl 添加到您的 composer.json 文件或

composer require "wykleph/curl"

如果您不使用 composer,也可以将 src 目录中的文件直接复制到您的项目中,但我建议这样做。

创建 Curler 实例。
$curler = new Curler('https://github.com/');

请注意,以下所有方法都可以链接在一起,除非另有说明

发布信息
$curler->post('fname', 'John')
    ->post('lname', 'Doe')
;
$curler->postArray(['fname'=>'John', 'lname'=>'Doe']);
从 POST 切换到 GET 请求
$curler->get();
设置头部
$curler->header('Connection', 'keep-alive')
    ->header('Host', 'github.com')
;
$curler->headerArray(['Connection'=>'keep-alive', 'Host'=>'github.com']);
设置用户代理或引用
$ua = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:40.0) Gecko/20100101 Firefox/40.0';

$curler->userAgent($ua)
    ->referer('http://github.com');
保存Cookies
$curler->cookieJar('SomeDirectory/testCookie');
跟随浏览器重定向
$curler->followRedirects();
压缩响应

您可以通过使用 compressedResponse() 来告诉 Curler 您期望从请求中获取压缩响应。

$curler->compressedResponse()
上传文件

只需提供表单输入名称和文件路径。

$curler->upload('file', 'filepath');
详细输出
$curler->verbose();
将响应写入文件

多请求支持即将推出。

$curler->writeResponse('someDirectory/Filename');

执行异步请求或多请求

您还可以发送异步请求!这可以通过添加 URL 到请求中(这保留了在 AsyncCurler 中设置的任何选项)或添加 cURL 处理程序到请求中(与添加 URL 相比,添加处理程序尚未充分测试)来实现。
正在进行中。

$curler = new AsyncCurler();

$urls = [
    'https://github.com/',
    'http://pastebin.com/',
    'https://google.com/',
    'http://yahoo.com/'
];

$headers = [
    'Connection'        =>      'keep-alive',
    'Accept'            =>      'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language'   =>      'en-US,en;q=0.5',
    'Content-Type'      =>      'application/x-www-form-urlencoded'
];

$cookieJar = '/home/user/testCookie';

$curler->followRedirects()          // Exactly how it sounds.
    ->headerArray($headers)         // Add an array of headers.
    ->cookieJar($cookieJar)         // Set a location for cookies.
    ->returnText()                  // Don't display response.  Get a text string.
    ->suppressRender()              // This will suppress the html from rendering if it is echoed.
    ->addUrl($urls)                 // Add urls to the multi-request..
    ->addUrl('https://php.ac.cn/');    // or add them individually.


$html = $curler->go()->getResponse();

var_dump($html);

请求调试

通过将 dryRun() 方法链接到方法链的末尾并输出结果,可以轻松调试请求。

这将输出类似以下内容(请考虑使用类似 Symfony 的 VarDumper 或 Laravel 的 dump 和 die - dd()

 [
   "url" => "https://github.com/"
   "cookieJarFile" => "/home/parker/gitCookie"
   "headers" => [
     "Connection" => "keep-alive"
     "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
     "Accept-Language" => "en-US,en;q=0.5"
     "Host" => "github.com"
     "Content-Type" => "application/x-www-form-urlencoded"
   ]
   "postfields" => []
   "poststring" => ""
   "handles" => []
   "options" => [
     "CURLOPT_FOLLOWLOCATION" => true
     "CURLOPT_COOKIEFILE" => "/home/parker/gitCookie"
     "CURLOPT_COOKIEJAR" => "/home/parker/gitCookie"
     "CURLOPT_RETURNTRANSFER" => true
     "CURLOPT_HTTPHEADER" => [
       0 => "Connection: keep-alive"
       1 => "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
       2 => "Accept-Language: en-US,en;q=0.5"
       3 => "Host: github.com"
       4 => "Content-Type: application/x-www-form-urlencoded"
     ]
   ]
   "curl_getinfo" => [
     "url" => "https://github.com/"
     "content_type" => null
     "http_code" => 0
     "header_size" => 0
     "request_size" => 0
     "filetime" => 0
     "ssl_verify_result" => 0
     "redirect_count" => 0
     "total_time" => 0.0
     "namelookup_time" => 0.0
     "connect_time" => 0.0
     "pretransfer_time" => 0.0
     "size_upload" => 0.0
     "size_download" => 0.0
     "speed_download" => 0.0
     "speed_upload" => 0.0
     "download_content_length" => -1.0
     "upload_content_length" => -1.0
     "starttransfer_time" => 0.0
     "redirect_time" => 0.0
     "redirect_url" => ""
     "primary_ip" => ""
     "certinfo" => []
     "primary_port" => 0
     "local_ip" => ""
     "local_port" => 0
   ]
 ]