4

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

Ben Collins
  • 221
  • 1
  • 4
  • 12

4 Answers4

8

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));
Community
  • 1
  • 1
Laith Shadeed
  • 4,271
  • 2
  • 23
  • 27
  • This is exactly what I have been trying to do for a while but I am struggling to implement it into PHP code which works correctly. Any pointers would be great. Appreciate all your responses guys – Ben Collins Nov 10 '11 at 12:04
  • Hi Laith, appreciate your time. Im placing the above into a PHP file and replacing the REST URL field with the API URL I wish to call but still no data is displayed when I load the PHP file (even though data is displayed when I navigate to the address through a web browser). Any Ideas? – Ben Collins Nov 10 '11 at 12:11
  • Have tried your amended version still with no luck. Im Stuck! – Ben Collins Nov 10 '11 at 12:25
  • Cant even echo that, I am obviously making a silly error here somewhere! – Ben Collins Nov 10 '11 at 12:34
  • If you can give me your REST API url, I can try it from my side – Laith Shadeed Nov 10 '11 at 12:40
  • I created a test API key, it works for me. Did you make sure that you pass &format=json? in the documentation it is 'format=XML' – Laith Shadeed Nov 10 '11 at 12:51
1

cUrl

cUrl is a command line tool for getting or sending files using URL syntax.

curl -o example.html www.example.com

file_get_contents

<?php
$homepage = file_get_contents('http://www.example.com/api/parameters');
echo $homepage;
?>
Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116
0

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

Another pretty cool client is the Buzz client https://github.com/kriswallsmith/Buzz It also plays nice with Symfony2 if that's of interest to you :)

Seb
  • 106
  • 1
  • 6
0

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.

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.

<?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:

<?php

$object = simplexml_load_string($data);

?>