2013-04-22 73 views
1

我想從諾基亞這裏獲取企業信息location api。以下是我的代碼:諾基亞這裏的位置api不能在php

$NokiaPlaceURL = 'http://places.nlp.nokia.com/places/v1/discover/search?app_id='.APP_ID.'&app_code='.APP_CODE.'&at='.$at.'&q='.$keyword.'&size=10'; 
     //echo $NokiaPlaceURL; exit; 
     $NokiaPlaceURL = file_get_contents($NokiaPlaceURL); 
     var_dump($NokiaPlaceURL);exit; 

它給出了以下錯誤。我該如何解決這個問題?

Warning: file_get_contents(http://places.nlp.nokia.com/places/v1/discover/search?app_id=---&app_code=---&at=40.708322%2C-74.008881&q=food&size=10) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 406 Not Acceptable in /opt/lampp/htdocs/test/nokia_here.php on line 29 
bool(false) 

感謝

回答

1

你需要設置一些HTTP頭爲您的要求。從API文檔中可以看出,您需要將Accept設置爲application/json(以JSON編碼的數據)或application/xhtml+xml(對於XML數據)。標頭被標記爲可選,但是,我懷疑PHP設置了自己的標頭,其中包括Accept,它沒有諾基亞API的預期價值。

但是,至於如何通過file_get_contents將頭添加到GET請求中,那麼您可以使用流上下文。例如:

$Options = array(
    'http' => array(
     'method' => 'GET', 
     'header' => 'Accept: application/json' 
    ) 
); 

$Context = stream_context_create($Options); 
$File = file_get_contents($URL, false, $Context); 
+0

HTTP標頭的最終列表可以在[地方API文檔]中找到(http://developer.here.com/docs/places/topics/http-request-headers.html)相關報價如下(重點是我的): _指定'application/json'產生json響應,而 'xhtml + xml'或等效(可能的通配符)導致類似於json的 HTML._ * *任何其他值將提示406響應。** – 2013-04-23 07:16:28

+0

是的,我想鏈接,但與所有框架最接近的直接鏈接,不失去導航是開始部分。 – pilsetnieks 2013-04-23 10:57:37