2012-08-31 61 views
1

我想通過php腳本來獲取yahoo woeid。爲什麼curl會返回null?

我可以從geo.places那裏訪問url'http://query.yahooapis.com/v1/public/yql?q=select * where text =「NewYork」& format = json'並獲取json文件在我的瀏覽器中。但是當我使用curl時它會返回null。

這就是代碼。

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; 
    } 

,當我嘗試訪問 'http://query.yahooapis.com/v1/public/yql?q=select *從geo.places其中文本= 「上海」 &格式= JSON' 的捲曲工作正常。

我無法修復它。

任何建議,將不勝感激! 謝謝。

+1

那是什麼? - > curl_setopt($ ch,CURLOPT_ – Oyeme

+0

$ data = curl_exec($ ch)或die($ this-> returnerror()); 即使curl_exec返回空白響應,die函數也會執行,所以最好使用這個, if($ data === false) { die($ this-> returnerror()); } – Tarun

+0

對不起,我忘記刪除該行了,我應該刪除該行,我正在嘗試修改該代碼。 – joe

回答

0
從您的代碼

剛剛得到的答案:

字符串 '不支持HTTP版本

'(長度= 210)

的PlaceFinder Web服務只支持HTTP GET方法。其他HTTP方法不受支持。

您不能使用CURL,因爲curl作爲POST方法工作。 請在這裏閱讀:http://developer.yahoo.com/geo/placefinder/guide/requests.html

+0

其實你可以用cURL來發出GET請求 – Jurgo

+0

是的,你說得對,默認情況下它是一個post方法。可以使用file_get_contents()。 – Oyeme

+0

但爲什麼我可以通過curl獲取此json的json'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text =「Shanghai」&format = json' – joe

0

你有一個開放的curl_setopt()。它低於:

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

我忘記刪除line.it應該被刪除。 – joe

3

您可以使用PHP的file_get_contents()。空格必須由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);