用 PHP 读取 REST API 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8079048/
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
Reading REST API Response in PHP
提问by Ben Collins
I am trying to read Raven SEO Tools API. It is a REST API and currently it is serving the data backup as an XML (or JSON if I choose) when I just request the URL through a web browser. What is the best method to get the response from their server into my own PHP script for me to then play around with.
我正在尝试阅读 Raven SEO Tools API。它是一个 REST API,目前当我通过 Web 浏览器请求 URL 时,它以 XML(或 JSON,如果我选择)的形式提供数据备份。从他们的服务器获取响应到我自己的 PHP 脚本供我使用的最佳方法是什么。
Any help much appreciated
非常感谢任何帮助
Cheers
干杯
回答by Laith Shadeed
If you only needs to retrieve a URL and parse its info. The easiest way is curl/JSON combination. Note that parsing JSON is faster than parsing XML.
如果您只需要检索 URL 并解析其信息。最简单的方法是 curl/JSON 组合。请注意,解析 JSON 比解析 XML 快。
- http://www.php.net/manual/en/function.curl-exec.php
- http://www.php.net/manual/en/function.json-decode.php
- http://www.php.net/manual/en/function.curl-exec.php
- http://www.php.net/manual/en/function.json-decode.php
Something simple as:
一些简单的事情:
$url = "http://api.raventools.com/api?key=B1DFC59CA6EC76FF&method=domains&format=json";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$json = curl_exec($ch);
if(!$json) {
echo curl_error($ch);
}
curl_close($ch);
print_r(json_decode($json));
But if you need to call other methods from this API such as DELETE/PUT, etc. Then to have a REST client in PHP is more elegant solution. A comparison on those clients can be found in PHP REST Clients
但是如果你需要从这个 API 调用其他方法,比如 DELETE/PUT 等,那么在 PHP 中拥有一个 REST 客户端是更优雅的解决方案。可以在PHP REST Clients 中找到对这些客户端的比较
I founded this code specifically for Raven API https://github.com/stephenyeargin/raventools-api-php
我专门为 Raven API 创建了这段代码https://github.com/stephenyeargin/raventools-api-php
Sample code:
示例代码:
require 'path/to/raventools-api-php/raventools-api-php.class.php';
$Raven = new RavenTools( 'B1DFC59CA6EC76FF' );
$method = 'domains';
$options = array('format'=> 'json');
$responseString = $Raven->getJSON($method, $options);
print_r(json_decode($responseString));
回答by Jon Winstanley
回答by Seb
Pecl's HTTPRequest class is a very nice client, I've been using it for a couple of Projects. http://pecl.php.net/package/pecl_http
Pecl 的 HTTPRequest 类是一个非常好的客户端,我已经将它用于几个项目。http://pecl.php.net/package/pecl_http
Another pretty cool client is the Buzz client https://github.com/kriswallsmith/BuzzIt also plays nice with Symfony2 if that's of interest to you :)
另一个非常酷的客户端是 Buzz 客户端https://github.com/kriswallsmith/Buzz如果您感兴趣,它也可以与 Symfony2 搭配使用 :)
回答by Jochen Van de Velde
You can use either one of them, but I think JSON is the easiest and more hassle-free, unless you use SimpleXML. The decision depends on the complexity of your data.
您可以使用其中任何一种,但我认为 JSON 是最简单、更轻松的,除非您使用 SimpleXML。该决定取决于您的数据的复杂性。
Given that the JSON returned by the API is valid you can convert it to an array or object by using PHP's json_decode()function.
鉴于 API 返回的 JSON 是有效的,您可以使用 PHP 的json_decode()函数将其转换为数组或对象。
<?php
# retrieve JSON from API here...
# i.e. it is stored in $data as a string
$object = json_decode($data);
$array = json_decode($data, true);
?>
In SimpleXML, it would be as follows:
在SimpleXML 中,它将如下所示:
<?php
$object = simplexml_load_string($data);
?>