max13/rest-manager

PHP的REST请求管理器,使用php-curl,旨在轻量级并且非常非常容易使用。哦BTW,我提到它非常容易使用了吗?

v1.0-p8 2016-07-06 23:16 UTC

This package is auto-updated.

Last update: 2024-09-27 13:54:39 UTC


README

备注

这是一个旧的README,当时库不能通过composer安装。我正在处理它,只有一些与autoload相关的更改。

要求

  • PHP >= 5.3
  • php-curl (如果需要,带有SSL)
  • 返回JSON编码数据的API(更多结构即将推出)
  • 大脑 >= Working

如何下载

有几种方法可以下载 MxRequestManager-PHP

  • 使用composer安装 ("max13/rest-manager": "dev-master")
  • 使用 git clone <repo> [<dest>] 克隆github仓库
  • 直接在github上下载zip文件
  • 自己试试找其他的吧 :/

然后将它放在你想要的地方(可读位置,以便加载)。

如何使用

显而易见的船长Obvious,这是README中最有趣的部分。

你可以在某个地方找到Doxygen文档,你将看到如何基本使用这个库。

首先,让我们看一个简单的例子。你有你的APIs (api.awsome-guy.com),你想要通过你的users资源使用GET检索成员列表。

JSON字符串将是

{
	users:[
		{
			"id": 4,
			"username": "foo"
		}, {
			"id": 12,
			"username": "bar"
		}
	]
}
<?php
// Adapt the path to your installation
require_once('MXRequestManager-PHP/MXRequestManager.php');

// Instanciate the manager, without the triling slash
$mx = new MXRequestManager('http://api.awsome-guy.com');

// Prepare your parameters, i.e a token
$params = array('token' => 'abcdefg');

// Make the request and return the status
$res = $mx->get('/users', $params);
?>

从那里,你必须验证$res,因为它可以是3种类型(因此也要用===检查类型)

// MXRequestManager error,
// errno are in the top of the lib file
if ($res === FALSE)
	die('Client error: '.$mx->errno());
if ($res === TRUE) // No JSON, may be a PHP Error
	die('Parse error: '.$mx->response('body'));

然后是你的检查(例如,如果存在属性errors),你就可以安全地使用$res作为stdClass

<?php
echo 'User n0: ' . $res->users[0]->id . "<br />\n";
foreach ($res->users as $user)
	echo $user->id . ' / ' . $user->username . "<br />\n";
?>

这个例子将输出

User n0: 4
4 / foo
12 / bar

就是这样。你现在可以使用MXRequestManager了!

如何检查整个响应?

这是简单的,你可以调用

<?php
echo $mx->rawResponse();
?>

如何检查头部?

MXRequestManager足够智能和聪明,可以让你简单地检查头部。

这里是一个头部示例

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 04 Feb 2013 21:49:22 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.3.21
Content-Encoding: gzip
Vary: Accept-Encoding

处理完毕后,每一行都被拆分并存储在一个数组中,可以通过键访问,对应于每行之前分号(:)的部分,除了第一行,它的键是Status和可以通过Code访问的HTTP代码

有一个内部的多维数组,包含2个根键:headersbody

  • headers包含一个关联数组,包含头部的值
  • body是作为字符串返回的正文

没有什么比例子更好了

<?php
// Adapt the path to your installation
require_once('MXRequestManager-PHP/MXRequestManager.php');

// Instanciate the manager, without the triling slash
$mx = new MXRequestManager('http://api.awsome-guy.com');

// Prepare your parameters, i.e a token
$params = array('token' => 'abcdefg');

// Make the request and return the status
$res = $mx->get('/users', $params);

// MXRequestManager error,
// errno are in the top of the lib file
if ($res === FALSE)
	die('Client error: '.$mx->errno());
if ($res === TRUE) // No JSON, may be a PHP Error
	die('Parse error: '.$mx->response('body'));

echo "HTTP Status: " . $mx->response('headers', 'Status');
echo "<br />\n";
echo "HTTP Code: " . $mx->response('headers', 'Code');
// Since PHP 5.4 you can use $mx->response()['headers']
echo "<br />\n";
echo "Response body: " . $mx->response('body');
?>

附加说明

将很快出现一种手册...