Wednesday, May 16, 2012

Working Yahoo PHP API


Get OAuth.php from  : http://oauth.googlecode.com/svn/code/php/OAuth.php


Install curl for PHP, remove the semi colon from "extension=php_curl.dll"



the query is given as a GET request or added in the URL as 


"http://xxx.php?q=searchterm"



Source : 


<?php


require("OAuth.php");  
   
$cc_key  = "YAHOO KEY HERE";  
$cc_secret = "SECRET KEY HERE";  
$url = "http://yboss.yahooapis.com/ysearch/web";  
$args = array();  
$q = $_GET["q"];
$args["q"] = $_GET["q"];  
$args["format"] = "xml";  
   
$consumer = new OAuthConsumer($cc_key, $cc_secret);  
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);  
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);  
$url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));  
$ch = curl_init();  


$headers = array($request->to_header());  
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
$rsp = curl_exec($ch);  
//$results = json_decode($rsp);  


//To print out the entire results
//print_r($results);


$root = new SimpleXMLElement($rsp);
$results = $root->web->results->result;
echo "<h1>Yahoo</h1><br /><br />";
foreach($results as $result)
{


        echo <<<EOF
<h2><a href='{$result->clickurl}'>{$result->title}</a></h2><br />
<div>{$result->abstract}</div>


<hr />


EOF;
}
?>

No comments:

Post a Comment