我正在尝试连接到RESTful Web服务,但我遇到了一些麻烦,特别是在通过PUT和DELETE发送数据时.使用cURL,PUT需要一个文件发送,而DELETE只是很奇怪.我完全有能力使用PHP的套接字支持编写客户端并自己编写HTTP头文件,但我想知道你们是否曾经使用或看过PHP的REST客户端?
Mat*_*ski 38
事实证明,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";
}
?>
Run Code Online (Sandbox Code Playgroud)
目前它使用curl但我可以将它切换到HTTP_Request或HTTP_Request2.
更新:看起来很多人都跳了这个.由于GitHub上的贡献者,Pest现在支持HTTP身份验证和许多其他功能.
Mic*_*ing 36
我写了一个名为Guzzle的PHP HTTP客户端.Guzzle是用于构建REST Web服务客户端的HTTP客户端和框架.您可以在其网站上找到有关Guzzle的更多信息,或直接访问github上的源代码:https://github.com/guzzle/guzzle
Guzzle提供大多数HTTP客户端提供的好东西(更简单的界面,所有HTTP方法,以及查看请求/响应),还提供其他高级功能:
唯一的缺点:它需要PHP 5.3.3
cee*_*yoz 13
我倾向于使用PHP的内置cURL支持.该CURLOPT_CUSTOMREQUEST选项允许你做PUT/ DELETE等
小智 8
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
我很久没找到优雅的解决方案了,不喜欢cURL实现,想出了我自己的.它支持HTTP身份验证,重定向,PUT等,因为它依赖于pecl http模块.
实现简单,易于扩展.
更多信息可以在这里找到: