2017 © Pedro Peláez
 

library curl-client

curl oop

image

cdcchen/curl-client

curl oop

  • Thursday, April 14, 2016
  • by cdcchen
  • Repository
  • 1 Watchers
  • 0 Stars
  • 407 Installations
  • PHP
  • 8 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 3 % Grown

The README.md

CURL Http Client

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解析返回的数据。目前支持以下格式:, (*1)

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

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

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

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

获取 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);
$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实例。, (*4)

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

$request->setFormat($value);

request会自动根据format的值来格式化data, (*5)

format的取值如下:, (*6)

  • 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);

The Versions

14/04 2016

dev-master

9999999-dev https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

 

The Development Requires

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

dev-develop

dev-develop https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

 

The Development Requires

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

1.0.0

1.0.0.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

1.0.1

1.0.1.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

1.0.2

1.0.2.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

1.0.3

1.0.3.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

1.0.4

1.0.4.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

1.0.5

1.0.5.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

1.0.6

1.0.6.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

1.0.7

1.0.7.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

1.0.8

1.0.8.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

v1.x-dev

1.9999999.9999999.9999999-dev https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

1.0.9

1.0.9.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Dong Chen

curl http httpclient curlclient phphttp

14/04 2016

2.0.0

2.0.0.0 https://github.com/cdcchen/php-plus

curl oop

  Sources   Download

BSD-4-Clause

The Requires

 

The Development Requires

by Dong Chen

curl http httpclient curlclient phphttp