perecedero/simple-curl

SimpleCurl 是使用 cURL 进行服务器之间通信的最简单方式

dev-master 2015-04-13 13:51 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:46:35 UTC


README

SimpleCurl 是使用 cURL 进行服务器之间通信的最简单方式。

要求

PHP 5+, 已启用 cURL

功能

  • 易于使用
  • 超级可配置
  • 自动解析响应

用法

	<?php
		require 'src/autoload.php';

		$c = new \Perecedero\SimpleCurl\Caller();

		$result = $c->call(array(
			//Options here
		));

		if ($result->code == 200) {

			$parsed_body = $result->get();
			//do something

		} else {

			//debug
			print_r($result->get('headers.sent') );
			print_r($result->get('headers.rcvd'));
			print_r($result->get('body')); //raw response body

		}

===

完整的调用选项列表

响应对象参考

===

示例

发起 GET 请求

	<?php
		require 'src/autoload.php';

		$c = new \Perecedero\SimpleCurl\Caller(array(
			'url.domain' => 'https://api.twitter.com',
			'parse.body.onerror' => true,
			'parse.body' => 'json',
		));

		$res = $c->call(array(
			'url.path' => '/1.1/search/tweets.json?q=@twitterapi',
		));

		if ($res->code != 200) {
			$errors = $res->get()->errors;
			print_r($errors);
		}

发起 POST 请求

	<?php
		require 'src/autoload.php';

		$c = new \Perecedero\SimpleCurl\Caller(array(
			'url.domain' => 'https://wordpresstheme.cn',
			'parse.body.onerror' => true,
			'parse.body' => 'json',
		));

		$res = $c->call(array(
			'url.path' => '/search/do-search.php',
			'post' => array('search'=> 'SimpleCurl')
		));

===

选项列表

url: 请求的 URL

  • 类型 string
  • 必需

url.domain: 请求的 URL

  • 类型 string
  • 如果没有传递选项 url 则必需

url.path: 与 url.domain 结合使用的 URL

  • 类型 string

user.pwd: 连接的登录详情字符串。其格式为:[用户名]:[密码]

  • 类型 String
  • 默认 null

method: 在进行 HTTP 请求时使用自定义请求方法而不是 "GET" 或 "HEAD"

  • 类型 String
  • 默认 null

header: 要发送的头列表

  • 类型 array
  • 默认 null

cookie: 要发送的 cookie 列表

  • 类型 mixed (array|string)
  • 默认 null

proxy: 用于隧道请求的 HTTP 代理。格式为 [主机]:[端口]

  • 类型 string
  • 默认 null

follow.location: 在 3xx 响应中,跟随服务器发送的任何 Location: 头

  • 类型 boolean
  • 默认 false

verify.ssl 验证 ssl 证书是否有效

  • 类型 boolean
  • 默认 false

timeout: 在建立通信后等待的秒数

  • 类型 integer
  • 默认 null

post: 通过 POST 发送的参数列表

  • 类型 array
  • 默认 null

upload.file 要通过 POST 发送的文件的路径

  • 类型 string
  • 默认 null

save.on 用于存储输出的文件路径,也用于下载文件

  • 类型 string
  • 默认 null

return.body 返回调用响应体,而不是布尔值作为函数返回值

  • 类型 boolean
  • 默认 true

parse.body: 解析响应。与 return.body=true 一起有效

  • 值 'auto', 'xml', 'json', 'json.assoc', 'raw', false
  • 类型 mixed
  • 默认 'auto'

parse.body.onerror: 在错误时解析响应(收到 4xx 或 5xx HTTP 状态码)。与 return.body=true 一起有效

  • 类型 boolean
  • 默认 false

===

###响应对象参考

您可以通过 get 方法获取有关结果的所有信息。

可能的方法参数包括

  • 'code' : 收到的 HTTP 状态码
  • 'headers.sent' : 请求上发送的头列表
  • 'headers.rcvd' => 作为响应的一部分接收的头列表
  • 'body': 原始响应体
  • 'parsed.body': 解析后的响应
  • 'latency': 完成请求的时间
  • 'size': 原始体大小

注意:如果没有任何参数,此方法将返回解析后的体

	$c = new \Perecedero\SimpleCurl\Caller(array(
		'url.domain' => 'https://api.twitter.com',
		'parse.body.onerror' => true,
		'parse.body' => 'json',
	));

	$res = $c->call(array(
		'url.path' => '/1.1/search/tweets.json?q=@twitterapi',
	));

	print_r ($res->get('headers.rcvd'));
	HTTP/1.1 400 Bad Request
	content-length: 62
	content-type: application/json;charset=utf-8
	date: Mon, 09 Mar 2015 17:59:20 UTC
	server: tsa_c
	set-cookie: guest_id=v1%3A142592396054742794; Domain=.twitter.com; Path=/; Expires=Wed, 08-Mar-2017 17:59:20 UTC
	strict-transport-security: max-age=631138519
	x-connection-hash: f6f703b23be46fc71c8d1ddd457e6fbf
	x-response-time: 21

您还可以使用 __get 方法来获取所有这些值

	$c = new \Perecedero\SimpleCurl\Caller(array(
		'url.domain' => 'https://api.twitter.com',
		'parse.body.onerror' => true,
		'parse.body' => 'json',
	));

	$res = $c->call(array(
		'url.path' => '/1.1/search/tweets.json?q=@twitterapi',
	));

	print_r($res->code);
	print_r($res->latency);
	print_r($res->body);