在PHP读取REST API响应 [英] Reading REST API Response in PHP

查看:328
本文介绍了在PHP读取REST API响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读乌鸦搜索引擎优化工具API。这是一个REST API,目前它所服务的数据备份作为XML(或JSON如果让我选择)时,我只是通过Web浏览器请求的URL。什么是从他们的服务器的响应到我自己的PHP脚本给我,然后玩的最好的方法。

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.

任何帮助非常AP preciated

Any help much appreciated

干杯

推荐答案

如果您只需要获取一个URL,并解析其信息。最简单的方法是卷曲/ JSON组合。需要注意的是解析JSON比解析XML要快。

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.


  1. http://www.php.net/manual/en/ function.curl-exec.php

  2. http://www.php.net/手动/ EN / function.json德code.php

  1. http://www.php.net/manual/en/function.curl-exec.php
  2. 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的其他方法,如删除/ PUT,等等。然后有一个REST客户端在PHP是更优雅的解决方案。这些客户端上的比较可以 PHP REST客户

我创办这个code专为乌鸦API https://github.com/stephenyeargin / raventools-API的PHP

I founded this code specifically for Raven API https://github.com/stephenyeargin/raventools-api-php

样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));

这篇关于在PHP读取REST API响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆