Reading REST API Response in 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.

Any help much appreciated

Cheers

Answers


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/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

I founded this code specifically for 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));

Need Your Help

Invoking UI thread from BeginGetResponse in Silverlight

.net silverlight windows-phone-7

I know how to do this in everyday .NET, but mucking round with WP7 development and Silverlight is driving me mad. An HttpWebRequest response must be done async, cool with that, but how can I invoke...

What do I need to use PayPal API?

e-commerce paypal

I a real noob when it comes to developing applications for PayPal. But, I have a client who needs me to build integration to paypal from his asp.net MVC website.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.