我有一个针对Yahoo BOSS V1的可靠测试页面,我需要与使用OAuth的V2代码兼容。这是V1页面:
<!--###V1 CODE###-->
<html>
<head><title>Yahoo BOSS v2 testing</title></head>
<body>
<form action="search.php" method="GET">
<label for="query"> Search </label>
<?php
echo '<input name="query" value="' . $_GET['query'] . '">';
?>
<input type="submit" value="Search">
</form>
<?php
$search_term = $_GET['query'];
if ($search_term != "") {
// Build search request.
$base_url = "http://boss.yahooapis.com/ysearch/";
$vertical = "web/";
$version = "v1/";
$search_term = urlencode($search_term);
$appid = "?appid=" . "your-app-id-here"; // replace with your app-id
$start = "&start=" . "0";
$count = "&count=" . "10";
$request_url = $base_url . $vertical . $version . $search_term . $appid . $start . $count;
// Send search request.
$curl_handle = curl_init($request_url);
curl_setopt($curl_handle,CURLOPT_URL, $request_url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
$raw_results = curl_exec($curl_handle);
curl_close($curl_handle);
$results_dict = json_decode($raw_results);
$results = $results_dict->ysearchresponse->resultset_web;
}
else {
$results = array();
}
?>
<!-- #### RESULTS BEGIN ####-->
<div id="results">
<?php
foreach ($results as $result) {
echo '<div class="result">';
echo '<h3 class="title"><a href='.$result->clickurl.'">'.$result->title.'</a></h3>';
echo '<p class="summary">'.$result->abstract.'</p>';
echo '</div>';
}
?>
</div>
<?php
// Some code...
//$start = "&start=" . "0";
$start_val = $_GET['start'];
if ($start_val == "") $start_val = 0;
$start = "&start=" . $start_val;
// Some more code...
$count_val = 10;
$count = "&count=" . $count_val;
if ($query != "") {
if ($start_val != 0) {
echo '<a href="?query='.$_GET['query'] . '&start='. (intval($start_val) - intval($count_val)) .'">previous</a>';
echo '<span> | </span>';
}
echo '<a href="?query='.$_GET['query'] . '&start='.(intval($start_val) + intval($count_val)) . '">next</a>';
}
?>
</body>
</html>
这个V1代码对我来说很有意义。我需要将BOSS V2 PHP代码集成到此中。但是怎么样?以下是V2 BOSS示例代码:
<!--###V2 CODE###-->
<?php
require("OAuth.php");
$cc_key = "your consumer key here";
$cc_secret = "your consumer secret here";
$url = "http://yboss.yahooapis.com/ysearch/web";
$args = array();
$args["q"] = "yahoo";
$args["format"] = "json";
$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);
print_r($results);
?>
我想我需要用V1中的$ cc_key和$ cc_secret替换V1中的$ appid。但是V2中的12-22行让我非常困惑。如果使用V2中的示例代码,V1中的代码会是什么样的?
答案 0 :(得分:0)
我自己试图解决这个问题。我很感激熟悉PHP的人的反馈。
<html>
<head><title>Yahoo BOSS v2 testing</title></head>
<body>
<form action="search.php" method="GET">
<label for="query"> Search </label>
<?php
echo '<input name="query" value="' . $_GET['query'] . '">';
?>
<input type="submit" value="Search">
</form>
<?php
require("OAuth.php");
$search_term = $_GET['query'];
if ($search_term != "") {
// Build search request.
$cc_key = "your consumer key here";
$cc_secret = "your consumer secret here";
$url = "http://yboss.yahooapis.com/ysearch/web";
$args = array();
$args["q"] = "yahoo";
$args["format"] = "json";
// Send search 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));
$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);
print_r($results);
}
else {
$results = array();
}
?>
<!-- #### RESULTS BEGIN ####-->
<div id="results">
<?php
foreach ($results as $result) {
echo '<div class="result">';
echo '<h3 class="title"><a href='.$result->clickurl.'">'.$result->title.'</a></h3>';
echo '<p class="summary">'.$result->abstract.'</p>';
echo '</div>';
}
?>
</div>
<?php
//-//$start = "&start=" . "0";
//$start_val = $_GET['start'];
//if ($start_val == "") $start_val = 0;
// $start = "&start=" . $start_val;
//$count_val = 10;
//$count = "&count=" . $count_val;
//if ($query != "") {
// if ($start_val != 0) {
// echo '<a href="?query='.$_GET['query'] . '&start='. (intval($start_val) - intval($count_val)) .'">previous</a>';
// echo '<span> | </span>';
// }
// echo '<a href="?query='.$_GET['query'] . '&start='.(intval($start_val) + intval($count_val)) . '">next</a>';
// }
?>
</body>
</html>