exgalibas / curl
php cURL 类
1.0
2018-04-10 06:25 UTC
Requires
- php: >=7.0
This package is not auto-updated.
Last update: 2024-09-28 10:52:18 UTC
README
Curl 是 php cURL 的包装器,链式调用且简单
安装
composer require exgalibas/curl
请求方法
GET|POST|OPTIONS|HEAD|PATCH|PUT|DELETE
用法
基本
$curl = new exgalibas\curl\Curl("http://example.com/index"); $curl->get(["name" => "exgalibas"]); // check curl error if (!$curl->error) { // output original curl response with header var_dump($curl->curlRawResponse); // output original curl response without header var_dump($curl->rawResponse); // output response processed by decoder var_dump($curl->response); // output real url var_dump($curl->url); // output request headers var_dump($curl->headers); // output curl options var_dump($curl->options); // output original curl response header var_dump($curl->rawResponseHeaders); // output processed response header var_dump($curl->responseHeaders); } else { // output error var_dump($curl->error); // output error code var_dump($curl->errorCode); // output error message var_dump($curl->errorMessage); // output curl error var_dump($curl->curlError); // output curl error code var_dump($curl->curlErrorCode); // output curl error message var_dump($curl->curlErrorMessage); // output http error var_dump($curl->httpError); // output http status code var_dump($curl->httpStatusCode); // output http status var_dump($curl->httpStatus); } // another style $curl = new curl\Curl(); $curl->url("http://example.com/index")->get(["name" => "exgalibas"]); // transfer raw string $curl->url("http://example.com/index")->get("a=b&c=d");
设置超时
$curl->url("http://example.com/index")->expire(10)->get(["name" => "exgalibas"]);
设置重试
$curl->url("http://example.com/index")->retry(4)->get(["name" => "exgalibas"]);
设置头部信息
$curl->url("http://example.com/index")->header("Content-Type", "application/json")->post(["name" => "exgalibas"]);
POST
$curl->url("http://example.com/index")->post(["name" => "exgalibas"]); // multidimensional array $curl->url("http://example.com/index")->post([ "message" => [ "name" => "exgalibas", "emial" => "exgalibas@gmail.com" ] ]); // raw string $curl->url("http://example.com/index")->post("abc"); //if server want to get the post string,do not use $_POST,file_get_contents("php://input") will work // post file $curl->url("http://example.com/index")->post([ "file" => new CURLFile(__DIR__ . '/index.php') ]); // another post file $curl->url("http://example.com/index")->post([ "file" => '@' . __DIR__ . '/index.php' ]) // post complicated data $curl->url("http://example.com/index")->post([ "name" => "exgalibas", "message" => [ "age" => 18, "size" => 18 ], "file" => [ "file1" => new CURLFile(__DIR__ . '/index.php'), "file2" => '@' . __DIR__ . '/index1.php' ], ]) // post json data $curl->url("http://example.com/index")->json()->post(["name" => "exgalibas"]); // another post json data $data = json_encode(["name" => "exgalibas"]); $curl->url("http://example.com/index")->post($data);
处理响应
在映射中有两个默认解码器,jsonDecoder 和 xmlDecoder,映射的格式为 [正则字符串 > 解码器]。如果您想使用默认解码器,响应头部的 Content-Type 必须是正确的类型,例如 application/json,您也可以指定其他解码器或在映射中添加新规则。
// specify decoder // $decoder must implements Decoder interface,$args will be transferred to $decoder $curl->url("http://example.com/index")->decoder($decoder)->decoderArgs($args)->get(); // jsonDecoder $args = [true, 12, JSON_BIGINT_AS_STRING]; //args will be used by json_decode() $curl->url("http://example.com/index")->decoderArgs($args)->get(); // add new rules, it will search the right decoder by comparing the Content-Type of response and the regex of map automatically $curl->url("http://example.com/index")->map('~^(?:text/|application/(?:atom\+|rss\+)?)xml~i', 'exgalibas\curl\XmlDecoder')->get();
待办事项
添加灵活的多 cURL