Getting Data from Yahoo Site Explorer Inbound Links API using PHP

In the first part of this two-part series, you learned the importance and principles of Yahoo Site Explorer’s inbound links API with respect to search engine optimization. If you read that part, you should already have your application ID, which will be used in your PHP script to make API calls. Also discussed in the first part is how to formulate the GET Request URL, and how to understand the responses from the inbound links API. In this part, you will start consolidating all of those inputs and implement what you’ve learned using a PHP server side scripting language.

The Basic Request and Response PHP Script

Yahoo Site Explorer’s inbound links API provides sample PHP code to make requests and for receiving responses. The API documentation for using PHP in the application can be found in two places, here and here. The sample script at the second link is a very basic PHP code and does NOT include processing of data.

One of the first things you need to declare is to turn on reporting of all errors in PHP, which will be useful when debugging complicated and API applications:

error_reporting(E_ALL);

Next you need to declare your request variable. In the first part, you learned that this request URL will be used as a GET request to Yahoo.

$request = ‘http://search.yahooapis.com/SiteExplorerService/V1/inlinkData?appid=TrnTAC3V34FlhDKeOScqpmw42Tu8B4BI8VUI7awsdcvfYIjJDwbtnb4rfNmmeQ–&query=http://www.php-developer.org&results=50&output=php';

The detailed discussion in how to formulate a GET request URL for Yahoo inbound links explorer API can be found in the part one of this tutorial.

Now that you have made a request, the vital third step is to receive the data from the Yahoo inbound links API and put into a string compatible for PHP processing. Using a PHP function called file_get_contents(), the entire response from Yahoo can be read into a string.

So it will be:

$response = file_get_contents($request);

IMPORTANT NOTE: Some web hosting companies, particularly those that offer free hosting plans, disable the file_get_contents PHP function. So it is highly recommended to check first with your hosting agency to make sure that the function has been enabled. If this is disabled, it appears to be impossible to develop useful PHP web applications with the API.

To detect problems with the response and receive process, you will need a conditional statement after the file_get_contents function:

//Step 4

if ($response === false) {

die(‘The request fails.’);

}

{mospagebreak title=Unserializing the data}

Okay, once the response has been dumped into a $response PHP variable, the data in this variable is not yet workable with PHP. This is because the data format is called “serialized,” which cannot be resolved to an easy analysis.

If $response variable (which is serialized) is displayed in a browser, this is how it would look:

The above raw data can be very hard to process, so in this case you will need to convert the above raw data format into an “array.” One effective method involves using the ” unserialize() ” function. So to unserialize the data:

//Step 5. Convert to unserialized

$unserialized = unserialize($response);

To display the unserialized array in the browser:

echo ‘<pre>';

print_r($unserialized);

echo ‘</pre>';

And finally, the completed basic PHP script, which will make a request, grab the response and convert to appropriate data format for processing (using the unserialized PHP function) will be:

<?php

//Step 1. Report possible errors for debugging

error_reporting(E_ALL);

//Step 2. Declare the request variable

$request=’http://search.yahooapis.com/SiteExplorerService/V1/inlinkData?appid=TrnTAC3V34FlhDKeOScqpmw42Tu8B4BI8VUI7awsdcvfYIjJDwbtnb4rfNmmeQ–&query=http://www.php-developer.org&results=50&output=php';

//Step 3. Read the responses from Yahoo inbound links API and put into a string.

$response = file_get_contents($request);

//Step 4

if ($response === false) {

die(‘The request fails.’);

}

//Step 5. Convert to unserialized

$unserialized = unserialize($response);

//Step 6. To display the array in the browser:

echo ‘<pre>';

print_r($unserialized);

echo ‘</pre>';

?>

So when the script is executed, it now uses a PHP array, which is easy to use and manipulate with your applications (see the screenshot below):

Note this is still raw data. An additional script is required to grab related data for analysis.

{mospagebreak title=Getting the data you need from the API raw response}

Inspection of the output screenshot of the code (see previous section) shows a complicated type of array commonly known in PHP as a “multi-dimensional array.” By analyzing this type of array we can grab our desired data from the Yahoo inbound links explorer API.

To process multi-dimensional arrays, let’s first do a little thought experiment. Suppose we wish to simulate the results in the previous screenshot using a PHP code; this is essential to our understanding of these arrays, which we can analyze further to grab the required data.

Again, you might find that the outermost array starts with [ResultSet]; this is the root array. Then the root array consists of four further arrays, namely:

[totalResultsAvailable]

[firstResultPosition]

[totalResultsReturned]

[Result]

The most important data can be found in the [Result] array. For example, the array [0] is the first back link data with the following innermost arrays:

[Title]

[Url]

[ClickURL]

To simulate an equivalent PHP code that will give the array structure in the earlier screenshot, start from the innermost array and work your way to the outermost array:

<?php

//This is the innermost array that contains the title tag, Url…etc

$innerarray0= array(‘Title’ => ‘This is a title0′, ‘Url’ => ‘This is the URL0′, ‘ClickUrl’ => ‘This is the ClickUrl0′);

$innerarray1= array(‘Title’ => ‘This is a title1′, ‘Url’ => ‘This is the URL1′, ‘ClickUrl’ => ‘This is the ClickUrl1′);

$innerarray2= array(‘Title’ => ‘This is a title2′, ‘Url’ => ‘This is the URL2′, ‘ClickUrl’ => ‘This is the ClickUrl2′);

//This is the counter array starting from 0 to 2, based on the number of results you have requested from the Yahoo explorer inbound links API

$arraycount= array(0 =>$innerarray0,1=>$innerarray1,2=>$innerarray2);

//The second outer array that contains the total number of backlinks, results, starting position

$outerarray = array(‘totalResultsAvailable’ => 569,’firstResultPosition’ => 1, ‘totalResultsReturned’ => 3,’Result’=> $arraycount);

//This is the outermost array [ResultSet]

$rootarray=array(‘ResultSet’=> $outerarray);

//PHP code to display this simulated PHP code that replicates the Yahoo explorer inbound links API response PHP array

echo ‘<pre>';

print_r($rootarray);

echo ‘</pre>';

echo ‘<br />';

?>

Below is the screenshot of the PHP code output:

Now the above simulated PHP code output screenshot has exactly the same array structure as the original unserialized response array of inbound links explorer API (see previous screenshot).

{mospagebreak title=Getting the data}

Based on the previous section’s analysis of PHP code, you can easily grab important data. For example, say you need the title tag of all backlink URLs.

Let:

$display= $unserialized ['ResultSet'];

$display1=$display['Result'];

This is the command to penetrate from the outermost array to the results array, and then, by the properties of combinational array, the title tag can be called as:

$display1[$x]['Title'];

Where $x is the counter of the array (example: 0 for the first back link, and 2 for the third back link.)

The final PHP code required to make a request to the Yahoo API, grab the response and finally display the title tag of all back link URLs to the browser is as follows:

<?php

//Step 1. Report possible errors for debugging

error_reporting(E_ALL);

//Step 2. Declare the request variable

$request=’http://search.yahooapis.com/SiteExplorerService/V1/inlinkData?appid= TrnTAC3V34FlhDKeOScqpmw42Tu8B4BI8VUI7awsdcvfYIjJDwbtnb4rfNmmeQ–&query=http://www.php-developer.org&results=3&output=php';

//Step 3. Read the responses from Yahoo inbound links API and put into a string.

$response = file_get_contents($request);

//Step 4

if ($response === false) {

die(‘The request fails.’);

}

//Step 5. Convert to unserialized

$unserialized = unserialize($response);

//Step 6. To display the array in the browser:

echo ‘<pre>';

print_r($unserialized);

echo ‘</pre>';

echo ‘<br />';

//DISPLAY THE TITLE TAG OF ALL BACKLINK URL

$display= $unserialized['ResultSet'];

$display1=$display['Result'];

//$display2=$display1[0]['Title'];

$x=0;

$count= sizeof($display1);

//echo $count;

while ($x < $count) {

echo $display1[$x]['Title'];

echo ‘<br />';

$x++;

}

?>

Take note that the WHILE loop has been used to iterate the grabbing of data from the first back links with array [0] to the maximum size of the array, determined by the sizeof() function.

[gp-comments width="770" linklove="off" ]

chat