I have a solid test page for Yahoo BOSS V1 that I need to make compatible with the V2 code which uses OAuth. Here is the V1 page:
<!--###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 开发者_StackOverflow$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>
This V1 code makes sense for the most part to me. I need to integrate the BOSS V2 PHP code into this. But how? Below is the V2 BOSS example code:
<!--###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);
?>
I think I need to replace $appid in V1 with $cc_key and $cc_secret from V2. But lines 12-22 in V2 have me very confused. What would the code in V1 look like if it used the example code from V2?
I've attempted to solve this myself. I would appreciate feedback from someone familiar with 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>
精彩评论