2014-03-07 106 views
-1
var value = 'New Delhi, India'; 

$yql_query_url = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text='.value.'&format=json'; 
$ch = curl_init($yql_query_url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, true); //Returns the headers 

$json = curl_exec($ch); //RESULT of YQL QUERY 

$status_code = array(); 
    preg_match('/\d\d\d/', $json, $status_code); 

    switch($status_code[0]) { 
     case 200: 
     echo "HTTP status of 200: Success!"; 
     // Success 
     break; 
     case 503: 
     die('Your call to Yahoo Web Services failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem 
      prevented us from returning data to you.'); 
     break; 
     case 403: 
     die('Your call to Yahoo Web Services failed and returned an HTTP status of 403. That means: Forbidden. You do not have permission to access   this resource, or are over your rate limit.'); 
     break; 
     case 400: 
     die('Your call to Yahoo Web Services failed and returned an HTTP status of 400. That means: Bad request. The parameters passed to the     service did not match as expected. The exact error is returned in the XML response.'); 
     break; 
     case 401: 
     die('Your call to Yahoo Web Services failed and returned an HTTP status of 401. That means: Authorization Required. The parameters passed   to the service did not match as expected. The exact error is returned in the XML response.'); 
     break; 

     case 999: 
     die('Your call to Yahoo Web Services failed and returned an HTTP status of 999. That means: Unable to process this request at this time.    The parameters passed to the service did not match as expected. The exact error is returned in the XML response.'); 
     break; 
     default: 
     die('Your call to Yahoo Web Services returned an unexpected HTTP status of:' . $status_code[0]); 
    } 
    preg_match('/{.*}/',$response, $json); 
    $result=json_decode($json,true); 

我收到錯誤Your call to Yahoo Web Services returned an unexpected HTTP status of:505雅虎API調用返回505錯誤

任何幫助,將不勝感激:)

回答

2

出自百科:

505不支持HTTP版本 服務器不支持請求中使用的HTTP協議版本。

您可以使用curl時強制HTTP版本:

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);//CURL will choose 
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//Use 1.0 
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);//Use 1.1 

選擇1.0的一個或1.1應該解決您的問題。

編輯:

也只注意到你的查詢字符串:

http://query.yahooapis.com/v1/public/yql?q=select *從geo.places其中文本= '值'。 & format = json

這是無效的。你會想要url_encode查詢。您也使用JavaScript語法代替PHP:

$value = 'New Delhi, India'; 

$query = "select * from geo.places where text='$value'"; 
$yql_query_url = 'http://query.yahooapis.com/v1/public/yql?q='.urlencode($query). '&format=json';