cdcchen/gif

1.0.0 2017-10-16 11:05 UTC

This package is not auto-updated.

Last update: 2024-11-16 16:03:01 UTC


README

composer 安装

composer require cdcchen/curl-client:^1.0.0

使用教程

第一步:实例化HttpRequest

$request = new HttpRequest();
$request->setUrl('http://www.baidu.com')

第二步:发起请求

$response = $request->send();

第三步:获取返回相关数据

返回的$response类型为HttpResponseHttpResponse会自动根据返回的Content-Type解析返回的数据。目前支持以下格式:

  • HttpRequest::FORMAT_JSON
  • HttpRequest::FORMAT_XML
  • HttpRequest::FORMAT_URLENCODED
  • HttpRequest::FORMAT_RAW_URLENCODED

如果已经明确知道返回数据的格式,也可以显式的指定$response的format属性:

$response->setFormat(HttpRequest::FORMAT_JSON);

然后可以直接通过$response->getData()来获取到解析之后的数据。

获取 Status Code

$response->getStatus()

获取原始 body 数据:

$response->getContent();

获取返回的 Headers

$response->hasHeader($name);
$response->getHeader($name);
$response->getHeaders();

获取 Cookies

$response->getCookies();

设置超时时间

$request->setConnectTimeout($value, $ms = false);
$request->setTimeout($value, $ms = false);

设置 HttpRequest 请求

设置请求方法

$request->setMethod('post');

设置请求参数

$request->setData(array $data);

设置post body

$request->setContent($content);

设置Header

$request->addHeader($name, $value);
$request->addHeaders($headers);
$request->removeHeader($name);
$request->clearHeaders();

Header shortcut

$request->setReferrer($referrer = true);
$request->setBasicAuth($username, $password);
$request->setUserPassword($username, $password);
$request->setUserAgent($agent);
$request->setFollowLocation($value = true, $maxRedirects = 5);
$request->setVersion($version);
$request->setAcceptEncoding($value);

设置Cookies

$request->addCookie($name, $value);
$request->addCookies($cookies);
$request->setCookieFile($file, $jar = null);

使用SSL

$reqeust->setSSL($peer = false, $host = 2, array $extraOptions = []);

上传文件

$request->addFile($input_name, $file, $mime_type = null, $post_name = null);
$request->addFiles($input_name, array $files, $mime_type = null, $post_name = null);
$request->clearFiles();

$file 可以为文件路径,也可以为CURLFile实例。

设置发起请求时body的格式化方式

$request->setFormat($value);

request会自动根据format的值来格式化data

format的取值如下:

  • HttpRequest::FORMAT_URLENCODED 默认
  • HttpRequest::FORMAT_RAW_URLENCODED
  • HttpRequest::FORMAT_JSON
  • HttpRequest::FORMAT_XML

配置 Curl Options

$request->addOption($name, $value);
$request->addOptions(array $options);
$request->removeOptions($options);
$request->clearOptions();
$request->resetOptions($setDefaultOptions = true);