CURL 请求

use zxf\Http\Curl;

Curl::instance()->setParams(['path'=>'pages/index/index'])->post($url,'json');

单例初始化

$curl=Curl::instance()

设置HTTP 请求头 http header

    /**
     * 设置http header
     *
     * @param array $header    设置的请求头
     * @param bool  $isAppend  是否追加
     * @param bool  $setLength 是否设置 Content-Length
     *
     * @return $this
     * @throws Exception
     */
$curl->setHeader(array $header, bool $isAppend = true, bool $setLength = false);

 

设置http请求的参数, 发送get、post、put等的请求body内容

    /**
     * 设置http请求的参数,get或post
     *
     * @param array  $params
     * @param string $data_type   数据类型 array|json|string
     * @param bool   $excludeZhCN ($data_type为string时生效)是否排除汉字转义 eg:数据[name='张三',age=25],true:不对中文进行编码得到 [name=张三&age=25] false:对中文编码得到 [name=%E5%BC%A0%E4%B8%89&age=25]
     *
     * @return $this
     * setParams( array('abc'=>'123', 'file1'=>'@/data/1.jpg'));
     * setParams( {'a'=>'str_a'});
     * @throws Exception
     */
$curl->setParams(array $params, string $data_type = 'array', bool $excludeZhCN = false);

设置cookie文件

    /**
     * 设置cookie文件的保存路径地址或者读取地址
     *
     * @param string $cookieFile
     * @param string $cookieJarFile
     *
     * @return $this
     */
$curl->setCookieFile(string $cookieFile = '', string $cookieJarFile = '');

设置cookie字符串

    /**
     * 设置cookie字符串
     *
     * @param string $cookieString
     *
     * @return $this
     */
$curl->setCookieString(string $cookieString);

设置http 超时

$curl->setTimeout(int $time = 3);

设置http 代理

$curl->setProxy(string $proxy);

设置http 代理端口

$curl->setProxyPort(int $port);

设置来源页面

    /**
     * 设置来源页面
     *
     * @param string $referer
     *
     * @return $this
     * @throws Exception
     */
$curl->setReferer(string $referer = "");

设置用户代理

$curl->setUserAgent(string $agent = "");

开启文件调试

    /**
     * 开启文件调试
     *
     * @param string $debugFile
     *
     * @return $this
     */
$curl->debug(string $debugFile);

关闭文件调试

    /**
     * 关闭调试
     * @return $this
     */
$curl->closeDebug();

设置是否返回对象

$curl->respObj(bool $flag = true);

复制句柄

$curl->copyCurl();

重置所有的预先设置的选项

$curl->reset();

http响应中是否显示header,1/true 表示显示

$curl->showResponseHeader($show);

设置证书路径

$curl->setCaPath(string $file);

获取cURL版本数组

$curl->version();

闭包方式 注入 Curl

    //  ...->inject(function($http, $ch){
    //      curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
    //  });
$curl->inject(Closure $func);

在请求之前调用闭包函数

    /**
     * 在请求之前调用闭包函数
     *
     * @param Closure $func
     *
     * @return $this
     * @example $http->before(function($http, $ch){
     *          // ...
     *          })
     */
$curl->before(Closure $func);

 

在请求之后调用闭包函数

    /**
     * 在请求之后调用闭包函数
     *
     * @param Closure $func
     *
     * @return $this
     * @example $http->after(function($http, $ch, $response){
     *          // ...
     *          })
     */
$curl->after(Closure $func);

GET请求

   /**
     * GET请求
     *
     * @param string $url
     * @param string $data_type 返回数据类型
     *
     * @return mixed
     *
     * Examples:
     * Curl::get('http://api.example.com/?a=123&b=456', 'json');
     *
     * @throws Exception
     */
$curl->get(string $url, string $data_type = 'json');

POST请求

    /**
     * POST请求
     *
     * @param string $url
     * @param string $data_type
     *
     * @return mixed
     *
     * Examples:
     * 
     * Http->post('http://api.example.com/?a=123',  'json');
     * Http->post('http://api.example.com/',  'json');
     * 文件post上传
     * Curl::post('http://api.example.com/', 'json');
     * 
     * @throws Exception
     */
$curl->post(string $url, string $data_type = 'json');

PUT请求

$curl->put(string $url, string $data_type = 'json');

delete请求

$curl->delete(string $url, string $data_type = 'json');

 

PATCH请求

$curl->patch(string $url, string $data_type = 'json');

 

上传文件

    /**
     * 上传文件
     *
     * @param string $url      上传地址
     * @param string $filePath 被上传文件绝对地址
     * @param string $name     上传字段名称;默认 media,eg: file
     * @param array  $params   上传的附加请求数据 。例如上传视频时候设置 description 等参数
     *
     * @return array|mixed
     * @throws Exception
     */
$curl->upload(string $url = '', string $filePath = '', string $name = '', array $params = []);

下载文件

    /**
     * 下载文件
     *
     * @param string $url      远程文件地址
     * @param string $filePath 存在在本地的地址
     *
     * @return array|mixed
     * @throws Exception
     */
$curl->download(string $url = '', string $filePath = '');

断点续传下载文件

    /**
     * 断点续传下载文件
     *
     * @param string $url      远程文件地址
     * @param string $filePath 存在在本地的地址
     * @param int    $range    下载节点
     *
     * @return array|mixed
     * @throws Exception
     */
$curl->downloadByRange(string $url = '', string $filePath = '', int $range = 0);

 

判断远程资源是否存在

$curl->exists(string $url = '');

异步将远程链接上的内容(图片或内容)写到本地

    /**
     * 异步将远程链接上的内容(图片或内容)写到本地
     *
     * @param string $url      远程地址
     * @param string $saveFile 保存在服务器上的文件名(e.g. /root/a/b.jpg)
     *
     * @return bool 当返回为true时,代表成功,反之,为失败
     */
$curl->putFileFromUrlContent(string $url, string $saveFile);

获取类中的属性

$resp->getAttr(string $attr = '');

请求是否成功(响应状态码是否为 2xx )

$resp->isSuccessful();

判断请求是否发生请求错误

$resp->isError();

返回响应状态码

$resp->getStatusCode();

响应内容

$resp->getBody();

响应头

$resp->getHeader(string $key = '');

获取 服务器信息

$resp->getServer(string $key = '');

是否重定向

$resp->hasRedirect();

所有响应错误内容

$resp->getError();

 

所有响应数据

$resp->getResp();