Cheerful Chanpreet
Cheerful Chanpreet

Reputation: 63

server not giving any response for curl code

i am new to curl and making a program which will search using yahoo search engine here is my code:-

<?php
        $handle=curl_init();
        curl_setopt($handle,CURLOPT_URL,"http://search.yahooapis.com/ImageSearchServ/V1/imageSearch");
        curl_setopt($handle,CURLOPT_POST,true);
        //curl_setopt($handle,CURLOPT_POSTFEILDS,"appid=$appID&query='water bear&results=5'");
        curl_setopt($handle,CURLOPT_RETURNTRANSFER,true);
        $response=curl_exec($handle);
        curl_close($handle);
        print_r($response);
    ?>

when i run this code the browser does not show any error message or any response it just bzy in requesting to the server like an infinite loop. can you please help me

Thanks in advance

Upvotes: 1

Views: 376

Answers (1)

edigu
edigu

Reputation: 10099

There are three problems with your code:

  • You're passing a string to post via curl. This is bad. You should prepare carefully the structured data to send.

Example:

 $data = array('appid'=>'12345',
               'query'=>'water bear',
               'results'=>5);
 $data = http_build_query($data, '', '&amp;'); // use native helper methods
 curl_setopt($handle,CURLOPT_POSTFIELDS,$data);
  • There is a typo in your code: CURLOPT_POST(FEI)LDS should be CURLOPT_POST(FIE)LDS.

  • Yahoo's restful search api url's changing in favor of the BOSS api. Your request uri should look something like this:

http://yboss.yahooapis.com/ysearch/{service,*}?q={keywords}

From the documentation:

BOSS API is an updated service that provides RESTful access to Web, Image, News, Spelling, and Blog search results with a simple pricing scheme based on usage. The service also provides qualifying developer's access to Yahoo! Search Advertising.

Upvotes: 1

Related Questions