开发者

Yahoo BOSS API Integration with OAuth

开发者 https://www.devze.com 2023-03-23 10:00 出处:网络
I am completely ne开发者_JAVA百科w to Yahoo! BOSS API. I have registered for BOSS API and got the required keys.

I am completely ne开发者_JAVA百科w to Yahoo! BOSS API. I have registered for BOSS API and got the required keys.

Now I want to integrate it with PHP. I don't know how to do this using OAuth service.

Can anyone guide me from starting point or provide me some reference or examples.

Thanks.


Here's one way to do it. This was scraped from http://tech.groups.yahoo.com/group/ysearchboss/message/3614

<?php

require("OAuth.php");


$cc_key  = "YOUR KEY HERE";
$cc_secret = "YOUR SECRET HERE";
$url = "http://yboss.yahooapis.com/ysearch/web";
$args = array();
$args["q"] = "yahoo";
$args["format"] = "xml";
$args["count"] = 1;

class NewsElement {
var $abstract; 
var $clickurl;
var $title;
var $language;
var $date;
var $source;
var $sourceurl;
var $url;

function NewsElement($aa)
{
foreach ($aa as $k=>$v) {
$this->$k = $aa[$k];
}
}
}


function parseMol($mvalues)
{
for ($i=0; $i < count($mvalues); $i++) {
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
}
return new NewsElement($mol);
}

function readXml($xmlResult)
{
// read the XML database of aminoacids
//$data = implode("", $xmlResult);
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $xmlResult, $values, $tags);
if(!xml_parser_free($parser))
die("Failed parsing");

// loop through the structures
foreach ($tags as $key=>$val) {
if ($key == "result") {
$molranges = $val;

    // each contiguous pair of array entries are the
    // lower and upper range for each molecule definition
    for ($i=0; $i < count($molranges); $i+=2) {

$offset = $molranges[$i] + 1;
$len = $molranges[$i + 1] - $offset;
$tdb[] = parseMol(array_slice($values, $offset, $len));
}
} else {
     continue;
}
}

return $tdb;

}

// Create oAuth request
$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));

// Initalize HTTP request

$ch = curl_init();
$headers = array($request->to_header());

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);Â 
curl_setopt($ch, CURLOPT_HEADER, 0);Â 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);Â 

$rsp = curl_exec($ch); 
$curl_result = curl_exec( $ch ) or die ( "could not execute the request" );
curl_close( $ch ); // close curl session

// Read the XML and write to a single array

$results = readXml($rsp);
print_r($results);

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消