Yahoo BOSS API与OAuth集成

时间:2011-07-27 10:19:45

标签: yahoo-api yahoo-boss-api

我对Yahoo!完全不熟悉BOSS API。我已经注册了BOSS API并获得了所需的密钥。

现在我想将它与PHP集成。我不知道如何使用OAuth服务。

任何人都可以从出发点开始指导我,或者为我提供一些参考或例子。

感谢。

1 个答案:

答案 0 :(得分:1)

这是一种方法。这是从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);

?>