How to correctly form yahoo boss url query

1.1k Views Asked by At

I am a novice programmer trying to understand how the api works. Been studying it for the last few days and been making some progress, but still need some help.

This seems to be the url to submit the query to, don't quite understand how to correctly form it?

$url = "http://yboss.yahooapis.com/ysearch/web";

If I want the results to come back as json, would this be correct?

$url = "http://yboss.yahooapis.com/ysearch/web?q={keywords}&format=json";

This really has me stumped, not sure at all how to get the actual query from the html form passed, would this be correct?

$url = "http://yboss.yahooapis.com/ysearch/web?q={keywords}";

If so, would I then need to include:

$args["keywords"]

If I could figure out how to correctly pass the query from the html form, I believe most of my other questions would also be answered.

Regarding this section, I assume these are the variables for various arguments that the api service allows. For each argument, do I have to include corresponding $args[""] code in the list below? For example, if I have &format=json in the url query, then I would also need to keep $args["format"] = json"; in the list below?

$args = array();
$args["q"] = "yahoo";
$args["format"] = "json";

Or say, if wanted to include:

$url = "http://yboss.yahooapis.com/ysearch/web?q={keywords}&abstract=long";

would I also need to include:

$args[abstract] = "long";

to the $args list above?

The part that really has me stumped is how do I get the actual query that is submitted through the html form to pass?

$url = "http://yboss.yahooapis.com/ysearch/web?q={keywords}

Do I then include a:

$args["keywords"]

to the $args list?

When I go to test it this is the results I get:

domain.com/search.php?q=car

stdClass Object ( [bossresponse] => stdClass Object ( [responsecode] => 200 [web] => stdClass Object ( [start] => 0 [count] => 50 [totalresults] => 112000000 [results] => Array ( [0] => stdClass Object ( [date] => [clickurl] => http://www.yahoo.com/ [url] => http://www.yahoo.com/ [dispurl] => www.yahoo.com [title] => Yahoo! [abstract] => Yahoo! home page with the latest breaking news and interesting articles, what items are trending now on the internet, links to other Yahoo! sites and local news and ... ) [1] => stdClass Object ( [date] => [clickurl] => http://mail.yahoo.com/ [url] => http://mail.yahoo.com/ [dispurl] => mail.yahoo.com [title] => Sign in to Yahoo! [abstract] => Yahoo! makes it easy to enjoy what matters most in your world. Best in class Yahoo! Mail, breaking local, national and global news, finance, sports, music, movies and ... ) [2] => stdClass Object ( [date] => [clickurl] => http://us.yahoo.com/ [url] => http://us.yahoo.com/ [dispurl] => us.yahoo.com [title] => Yahoo! [abstract] => A new welcome to Yahoo!. The new Yahoo! experience makes it easier to discover the news and information that you care about most. It's the web ordered for you. ) [3] => stdClass Object ( [date] => [clickurl] => http://sports.yahoo.com/ [url] => http://sports.yahoo.com/ [dispurl] => sports.yahoo.com [title] => Yahoo! Sports - Sports News, Scores, Rumors, Fantasy Games ... [abstract] => All the latest sports news, scores, rumors, fantasy games, and more ) [4] => stdClass Object ( [date] => [clickurl] => http://my.yahoo.com/ [url] => http://my.yahoo.com/ [dispurl] => my.yahoo.com [title] => Sign in to Yahoo! [abstract] => My Yahoo is a customizable web page with news, stock quotes, weather, and many other features. ) [5] => stdClass Object ( [date] => [clickurl] => http://dir.yahoo.com/ [url] => http://dir.yahoo.com/ [dispurl] => dir.yahoo.com [title] => Yahoo! Directory [abstract] => Help us improve the Yahoo! Directory - Share your ideas ) [6] => stdClass Object ( [date] => [clickurl] => http://search.yahoo.com/ [url] => http://search.yahoo.com/ [dispurl] => search.yahoo.com [title] => Yahoo! Search - Web Search [abstract] => The search engine that helps you find exactly what you're looking for. Find the most relevant information, video, images, and answers from all across the Web. ) [7] => stdClass Object ( [date] => [clickurl] => http://news.yahoo.com/ [url] => http://news.yahoo.com/ [dispurl] => news.yahoo.com [title] => Yahoo! News - Latest News & Headlines [abstract] => The latest news and headlines from Yahoo! News. Get breaking news stories and in-depth coverage with videos and photos. ) [8] => stdClass Object ( [date] => [clickurl] => http://autos.yahoo.com/ [url] => http://autos.yahoo.com/ [dispurl] => autos.yahoo.com [title] => New car pictures, prices and reviews - Yahoo! Autos [abstract] => See new car pictures, find out new car prices and read new car reviews on Yahoo! Autos. Compare cars and get a free price quote from dealers near you. Check out Clear ... ) [9] => stdClass Object ( [date] => [clickurl] => http://us.m.yahoo.com/ [url] => http://us.m.yahoo.com/ [dispurl] => us.m.yahoo.com [title] => Yahoo! Mobile [abstract] => At your Yahoo!

Obviously the query is not being passed properly because the result is all about Yahoo instead of the query "car". And it looks like it is an unformatted json response because I know xml is way different. Any links to a good tutorial on json would be great.

If I could figure out how to get the actual query passed from the html form, I believe it would answer many of these questions.

Below is what I believe to be the default code that yahoo supplies to display the results:

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

Would I need to set up some type of json code for that? Could an example or link be provided to help me better understand json, I know the basis of it is key value pairs, but have never worked with json before, not sure where to start....

I apologize for such a long question, but I have many questions and instead of submitting mutliple questions, I figured 1 long question would be best.

1

There are 1 best solutions below

0
On

This is what I'm using for Yahoo BOSS API in PHP, I hope it helps

<?php
// This enable debugging
ini_set('display_errors', 'On');
error_reporting(E_ALL);

require("OAuth.php"); // You will need to make your own OAuth.php

$cc_key = "---FILL THIS IN WITH YOUR INFO---";
$cc_secret = "---FILL THIS IN WITH YOUR INFO---";
$url = "http://yboss.yahooapis.com/ysearch/web"; // This can be tweaked to search images, videos etc
$args = array();
$args["q"] = "--FILL THIS IN WITH WHAT YOU WANT TO SEARCH FOR---"; // search operator, fill in with query value
$args["count"] = "10"; // Default is 50, restricts results down to 10
$args["web.sites"] = "";  // comma separated value, optional -- usnews.com,nytimes.com, etc
$args["format"] = "json"; // Default is json, options are xml or json, code below only works for JSON at the moment


$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));
echo $url . "<br>";
$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); // Decodes JSON
print_r($results); // Prints results to screen
?>