Getting Data from Yahoo Site Explorer Inbound Links API using PHP (Page 1 of 4 )
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.');
}