我正在尝试阅读 Raven SEO Tools API。它是一个 REST API,目前当我通过 Web 浏览器请求 URL 时,它以 XML(或 JSON,如果我选择)的形式提供数据备份。将他们的服务器的响应获取到我自己的 PHP 脚本中供我使用的最佳方法是什么?
非常感谢任何帮助
Cheers
如果您只需要检索 URL 并解析其信息。最简单的方法是curl/JSON组合。请注意,解析 JSON 比解析 XML 更快。
- http://www.php.net/manual/en/function.curl-exec.php 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.json-decode.php
简单的事情如下:
$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));
但如果您需要从此 API 调用其他方法,例如 DELETE/PUT 等。那么在 PHP 中使用 REST 客户端是更优雅的解决方案。这些客户的比较可以在PHP REST 客户端 https://stackoverflow.com/questions/894268/php-rest-clients
我专门为 Raven API 创建了这段代码https://github.com/stephenyeargin/raventools-api-php https://github.com/stephenyeargin/raventools-api-php
示例代码:
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));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)