我正在尝试连接到 RESTful Web 服务,但遇到了一些麻烦,尤其是在通过 PUT 和 DELETE 发送数据时。使用 cURL,PUT 需要发送文件,而 DELETE 很奇怪。我完全有能力使用 PHP 的套接字支持编写客户端并自己编写 HTTP 标头,但我想知道你们是否曾经使用或见过 PHP 的 REST 客户端?
8 回答
事实证明,Zend_Rest_Client 根本不是 REST 客户端——例如,它不支持 PUT 和 DELETE 方法。在尝试将其与实际的 RESTful 服务一起工作后,我受够了并为 PHP 编写了一个合适的 REST 客户端:
http://github.com/educoder/pest
它仍然缺少一些东西,但如果它被捡起,我会做更多的工作。
以下是 OpenStreetMap REST 服务的使用示例:
<?php
/**
* This PestXML usage example pulls data from the OpenStreetMap API.
* (see http://wiki.openstreetmap.org/wiki/API_v0.6)
**/
require_once 'PestXML.php';
$pest = new PestXML('http://api.openstreetmap.org/api/0.6');
// Retrieve map data for the University of Toronto campus
$map = $pest->get('/map?bbox=-79.39997,43.65827,-79.39344,43.66903');
// Print all of the street names in the map
$streets = $map->xpath('//way/tag[@k="name"]');
foreach ($streets as $s) {
echo $s['v'] . "\n";
}
?>
目前它使用 curl 但我可以将其切换到 HTTP_Request 或 HTTP_Request2 。
更新:看起来有不少人对此表示赞同。由于 GitHub 上的贡献者,Pest 现在支持 HTTP 身份验证和许多其他功能。
我编写了一个名为 Guzzle 的 PHP HTTP 客户端。Guzzle 是一个 HTTP 客户端和框架,用于构建 REST Web 服务客户端。您可以在其网站上找到有关 Guzzle 的更多信息,或直接访问 github 上的源代码:https ://github.com/guzzle/guzzle
Guzzle 提供了大多数 HTTP 客户端提供的好东西(更简单的界面、所有 HTTP 方法以及查看请求/响应),但还提供了其他高级功能:
- 流式实体主体
- 指数退避
- 内置缓存转发代理
- 饼干
- 日志记录
- 托管持久连接
- 并行请求
- 身份验证
- 允许您实现任意身份验证方案的插件架构
- 从 JSON 服务描述文件自动生成客户端 API
唯一的缺点:它需要 PHP 5.3.3
我倾向于只使用 PHP 的内置cURL 支持。该CURLOPT_CUSTOMREQUEST
选项允许您执行PUT
/DELETE
等。
其余客户端的 php 中的简单示例 - 更新如下:
<?php
$url ="http://example.com";
$data = "The updated text message";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); //for updating we have to use PUT method.
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>
其余客户端的 php 中的简单示例 - 删除 categoryid=xx 如下:
<?php
$url ="http://example.com/categoryid=xx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>
我很长一段时间都找不到优雅的解决方案,不喜欢 cURL 实现,想出了我自己的。它支持 HTTP 身份验证、重定向、PUT 等,因为它依赖于 pecl http 模块。
实现很好,简单,易于扩展。
更多信息可以在这里找到:
复活话题,发现这个库https://github.com/Respect/Rest/很好用,但是网上的例子很少:
require_once 'bootstrap.php';
require_once 'Respect/Rest/Router.php';
require_once 'Respect/Rest/Request.php';
use Respect\Rest\Router;
$router->post('/myApp/', function() {
$data_back = json_decode(file_get_contents('php://input'));
// var_dump($data_back);
return json_encode($data_back);
});
$router->get('/myApp/*', function($id = null) {
$json = json_encode(MyService::getInstance()->list());
return utf8_encode($json);
});
$router->put('/myApp/*', function($id = null) {
return 'Update: ' . $id;
});
$router->delete('/myApp/*', function($id = null) {
return 'Delete: ' . $id;
});
最近出现的是Zend\Http\Client,它是 Zend Framework 2 的一部分。
可通过 composer 安装(尽管在撰写本文时,不是通过 Packagist;仍然需要使用 Zend 的自定义包存储库http://packages.zendframework.com/)。