2

I want to get the yahoo woeid by php script.

I can access the url 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="NewYork"&format=json' and get the json file in my browser.But when I use the curl it return null.

that's the code.

    public function getwoeid(){
        $ch = curl_init();
        $url = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="'.$this->cityname.'"&format=json';
        //echo $url;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data=curl_exec($ch) or die($this->returnerror()); 
        curl_close($ch);
        $array = json_decode($data, true);
        return $array;
    }

when I try to access the 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="上海"&format=json' the curl works fine.

I can't fix it.

any suggestion would be appreciated! Thanks.

4
  • 1
    What's that ?? -> curl_setopt($ch, CURLOPT_ Commented Aug 31, 2012 at 9:06
  • $data=curl_exec($ch) or die($this->returnerror()); Die function will execute even if curl_exec return blank response so better use this, if($data === false) { die($this->returnerror()); } Commented Aug 31, 2012 at 9:07
  • sorry I forget to delete the line. I should delete the line.I'm trying to modify the code. Commented Aug 31, 2012 at 9:22
  • What codepage U use? Utf-16?, Try urlencode your $url Commented Sep 10, 2012 at 7:05

3 Answers 3

3

You can just use PHP's file_get_contents(). The spaces must be encoded by urlencode();

$url = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="'.$this->cityname.'"&format=json';
$data = file_get_contents(urlencode($url));
$array = json_decode($data, true);
Sign up to request clarification or add additional context in comments.

Comments

0

Just got answer from your code:

string 'HTTP Version Not Supported

�' (length=210)

The PlaceFinder Web service supports only the HTTP GET method. Other HTTP methods are not supported.

You cannot use CURL,because curl works as a POST method. Please read here: http://developer.yahoo.com/geo/placefinder/guide/requests.html

5 Comments

actualy you can use cURL to make GET requests.
Yes,you are right,by default it's a post method.Better to use file_get_contents() for it.
but why I can get the json by curl for this url 'query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="Shanghai"&format=json'
@joe u can,u should use GET metod instead of post.
I've fix it.actually I don't know the reason! public function getwoeid(){ $defaults = array( CURLOPT_URL => 'query.yahooapis.com/v1/public/yql?'. http_build_query(array('q'=>'select * from geo.places where text="'.$this->cityname.'"','format'=>'json')), CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => TRUE ); $ch = curl_init(); curl_setopt_array($ch, $defaults); if( ! $result = curl_exec($ch)) { $this->returnerror(); } curl_close($ch); $array = json_decode($result, true); return $array; }
0

You have an open curl_setopt(). it is below:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ <------

1 Comment

I forget to delete the line.it should be deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.