Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

PHP with Yahoo API

Dear experts,

I have been looking for a simple PHP code for connect and search with Yahoo! search API.

I registered to Yahoo! ID and got one but I did not find an easy way to do the search.

The aim of this question is to make search from PHP within my local server with Yahoo! and received the search list along with the name,description and url of each site.

I will be grateful for any help.

Thanks in advance.

Regards.
0
Tree_PRO
Asked:
Tree_PRO
  • 5
  • 4
1 Solution
 
elrond_IIICommented:
If you use the RESTful WebService Documented on this page you have several options:
http://developer.yahoo.com/search/web/V1/webSearch.html

For PHP I actually suggest you use cURL (www.php.net/curl), Yahoo actually has quite a nice tutorial how to make these requests (see: http://developer.yahoo.com/php/howto-reqRestPhp.html ). What you now do is just create links like the first documentation suggests, and send them on the server with php curl statements.

There are a lot of possible configurations for curl, for all options see the php.net/curl documentation site. It is f.e. possible do not directly include the response from yahoo into your own page, but just store the answer into a php variable.... more on that, if you ask again ;)




$request =  'http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=madonna&results=2'; 
$session = curl_init($request);
curl_setopt($session, CURLOPT_HEADER, false); 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session); 
curl_close($session);

Open in new window

0
 
Tree_PROAuthor Commented:
Hello,

Thanks for the reply, I saw the links but it looks some confuse. I don't know from where can I start. Could you please help with an example code in PHP that search from Yahoo! from a word such as Experts and then save the result into variables for example : the name of site in $name and the url of site in $url. Therefore, I could control the display of result and its formating.

I will be glad for your reply.

Regards
0
 
Tree_PROAuthor Commented:
Dear Experts ,

I will be happy for any more comments about the questions.

Regards
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
elrond_IIICommented:
Actually, yesterday was carneval in austria, from where I am from, so that was time to party, so sry for my late reply.... ;)

Actually, I don't really understand what you mean with $name, but that's probably because of partying yesterday @_@, so the code doesn't use that.....

The code below is definitely not a peace of art, but should only be a starter:

// Construct the url first
 
$baseurl = "http://search.yahooapis.com/WebSearchService/V1/webSearch";
$appid = "appid=YOURAPPIDHERE";
$search = "query=Experts";
$url = $baseurl . "?" . $appid . "&" . $search;
 
// make the php get request, store the response in the variable $answer
$session = curl_init($url);
curl_setopt($session, CURLOPT_HEADER, false)
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$answer = curl_exec($session);
curl_close($session);
 
// show the xml answer:
print_r($answer);

Open in new window

0
 
Tree_PROAuthor Commented:
Hi,

I hope you had a nice carnival yesterday :)

Thanks for the code, the code is easy to understand but unfortunately it did not work with me, I got this error while open the file:
Parse error:  syntax error, unexpected T_STRING in /opt/lampp/htdocs/yahoo.php on line 12

I meant by $name is that to save the results into some variables so I could display them later by echo command.
Thanks in advance.

0
 
elrond_IIICommented:
hm, I copied that code from some part of my code, but I had to modify 3 lines, somehow, it seems I hat forgotten a ;, didn't I?

now it should work
// Construct the url first
 
$baseurl = "http://search.yahooapis.com/WebSearchService/V1/webSearch";
$appid = "appid=YOURAPPIDHERE";
$search = "query=Experts";
$url = $baseurl . "?" . $appid . "&" . $search;
 
// make the php get request, store the response in the variable $answer
$session = curl_init($url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$answer = curl_exec($session);
curl_close($session);
 
// show the xml answer:
print_r($answer);

Open in new window

0
 
Tree_PROAuthor Commented:
Hi,

Sorry, it did not work again, I received this error:
Fatal error: Call to undefined function curl_init() in D:\xampp\htdocs\y\index.php on line 10

I tried to modify it but I did not success. I will be glad if you could please modify it to work.
Thanks in advance.
0
 
elrond_IIICommented:
you get this error if curl is not enabled in you php installation.... do you use an own server? If yes, there is a php-curl package you can add to php.... if you are unshure: make a echo phpinfo() call and look if curl is enabled in there
0
 
Tree_PROAuthor Commented:
You are right, I did not have the php-curl package. I installed it and it works fine now.

Thanks mate for helping me :)

Regards.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now