PHP REST 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/894268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:14:47  来源:igfitidea点击:

PHP REST Clients

phprestclient

提问by Jamie Rumbelow

I'm trying to connect to a RESTful web service, but I'm having some troubles, especially when sending data over PUT and DELETE. With cURL, PUT requires a file to send, and DELETE is just weird. I'm perfectly capable of writing a client using PHP's socket support and writing the HTTP headers myself, but I wanted to know whether you guys have ever used or seen a REST client for PHP?

我正在尝试连接到 RESTful Web 服务,但遇到了一些麻烦,尤其是在通过 PUT 和 DELETE 发送数据时。使用 cURL,PUT 需要发送文件,而 DELETE 很奇怪。我完全有能力使用 PHP 的套接字支持编写客户端并自己编写 HTTP 标头,但我想知道你们是否曾经使用或见过 PHP 的 REST 客户端?

回答by Matt Zukowski

So as it turns out, Zend_Rest_Client isn't a REST client at all — it does not support the PUT and DELETE methods for example. After trying to kludge it into working with an actual RESTful service I got fed up and wrote a proper REST client for PHP:

因此,事实证明, Zend_Rest_Client 根本不是 REST 客户端——例如,它不支持 PUT 和 DELETE 方法。在尝试将其与实际的 RESTful 服务结合使用后,我受够了并为 PHP 编写了一个合适的 REST 客户端:

http://github.com/educoder/pest

http://github.com/educoder/pest

It's still missing a few things but if it gets picked up I'll put some more work into it.

它仍然缺少一些东西,但如果它被捡起,我会投入更多的工作。

Here's a usage example with the OpenStreetMap REST service:

这是 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";
}

?>

Currently it uses curl but I may switch it to HTTP_Request or HTTP_Request2 down the line.

目前它使用 curl,但我可能会将其切换为 HTTP_Request 或 HTTP_Request2。

Update:Looks like quite a few people have jumped on this. Pest now has support for HTTP authentication and a bunch of other features thanks to contributors on GitHub.

更新:看起来很多人已经跳上了这个。由于 GitHub 上的贡献者,Pest 现在支持 HTTP 身份验证和一系列其他功能。

回答by Michael Dowling

I wrote a PHP HTTP client called Guzzle. Guzzle is an HTTP client and framework for building REST webservice clients. You can find more information about Guzzle on its website, or go straight to the source on github: https://github.com/guzzle/guzzle

我编写了一个名为 Guzzle 的 PHP HTTP 客户端。Guzzle 是用于构建 REST Web 服务客户端的 HTTP 客户端和框架。你可以在它的网站上找到更多关于 Guzzle 的信息,或者直接去 github 上的源码:https: //github.com/guzzle/guzzle

Guzzle provides goodies that most HTTP clients provide (a simpler interface, all of the HTTP methods, and viewing the request/response), but also provides other advanced features:

Guzzle 提供了大多数 HTTP 客户端提供的优点(更简单的界面、所有 HTTP 方法和查看请求/响应),但还提供其他高级功能:

  • streaming entity bodies
  • exponential backoff
  • a built-in caching forward proxy
  • cookies
  • logging
  • managed persistent connections
  • parallel requests
  • OAuth
  • a plugin architecture that allows you to implement arbitrary authentication schemes
  • Autogenerating a Client API from a JSON service description file
  • 流实体
  • 指数退避
  • 内置缓存转发代理
  • 饼干
  • 日志记录
  • 托管持久连接
  • 并行请求
  • 身份验证
  • 允许您实现任意身份验证方案的插件架构
  • 从 JSON 服务描述文件自动生成客户端 API

The only drawback: It requires PHP 5.3.3

唯一的缺点:它需要 PHP 5.3.3

回答by ceejayoz

I tend to just use PHP's built-in cURL support. The CURLOPT_CUSTOMREQUESToption allows you to do PUT/DELETEetc.

我倾向于只使用 PHP 的内置cURL 支持。该CURLOPT_CUSTOMREQUEST选项允许您执行PUT/DELETE等操作。

回答by ceejayoz

simple example in php for the rest client - updating is given below:

其余客户端的 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);
?>

simple example in php for the rest client - deleting of categoryid=xx is given below:

其余客户端的 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);
?>

回答by shurikk

I wasn't able to find elegant solution for a long time, didn't like cURL implementations, came up with my own. It supports HTTP authentication, redirects, PUT, etc. because it relies on pecl http module.

很长一段时间我都找不到优雅的解决方案,不喜欢 cURL 实现,想出了自己的解决方案。它支持 HTTP 身份验证、重定向、PUT 等,因为它依赖于 pecl http 模块。

Implementation is nice and simple, easy to extend.

实现很好,很简单,易于扩展。

More information can be found here:

更多信息可以在这里找到:

回答by catsby

I've had good success with Zend Rest Client

我在Zend Rest Client 上取得了很好的成功

回答by Evertonvps

Resurrecting the topic, I found this library https://github.com/Respect/Rest/is very easy to use, but there are few examples on the web:

重温正题,发现这个库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;
 });

回答by David Weinraub

A recent arrival is Zend\Http\Client, part of the Zend Framework 2.

最近出现的是Zend\Http\Client,它是 Zend Framework 2 的一部分。

Installable via composer (though, as of this writing, not via Packagist; still need to use Zend's custom package repository http://packages.zendframework.com/).

可通过 composer 安装(不过,在撰写本文时,不是通过 Packagist;仍然需要使用 Zend 的自定义包存储库http://packages.zendframework.com/)。