2013-05-31 83 views
0

此問題類似於:Freebase API call works in browser but fails with curl但沒有答案。Freebase API在瀏覽器中工作,但不在PHP中

我有這個網址,在瀏覽器的工作原理:

https://www.googleapis.com/freebase/v1/search?query=Cee+Lo+Green&filter=%28all+type%3A%2Fmusic%2Fartist+created%3A%22The+Lady+Killer%22%29&limit=10&key=my_key_here 

返回:

{"status":"200 OK","result":[{"mid":"/m/01w5jwb","id":"/en/cee_lo_1974","name":"Cee Lo Green","notable":{"name":"Singer-songwriter","id":"/m/016z4k"},"lang":"en","score":814.733643}],"cost":6,"hits":2} 

然而,在PHP中,下面的代碼無法正常工作:

<?php 
namespace com\voting; 

class Freebase{ 

    public function query(){ 
     $service_url = 'https://www.googleapis.com/freebase/v1/search'; 
     $params = array(
       'query' => 'Cee Lo Green', 
       'filter' => '(all type:/music/artist created:"The Lady Killer")', 
       'limit' => 10, 
       'key' => API_KEY ); 
     $url = $service_url . '?' . http_build_query($params); 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_VERBOSE, true); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     $response = json_decode(curl_exec($ch), true); 
     curl_close($ch); 
     return $response; 
    } 
} 
?> 

一卷捲髮,給出這個:

array (size=22) 
    'url' => string 'https://www.googleapis.com/freebase/v1/search?query=Cee+Lo+Green&filter=%28all+type%3A%2Fmusic%2Fartist+created%3A%22The+Lady+Killer%22%29&limit=10&key=my_key_here' (length=191) 
    'content_type' => null 
    'http_code' => int 0 
    'header_size' => int 0 
    'request_size' => int 0 
    'filetime' => int -1 
    'ssl_verify_result' => int 0 
    'redirect_count' => int 0 
    'total_time' => float 0.047 
    'namelookup_time' => float 0 
    'connect_time' => float 0.063 
    'pretransfer_time' => float 0 
    'size_upload' => float 0 
    'size_download' => float 0 
    'speed_download' => float 0 
    'speed_upload' => float 0 
    'download_content_length' => float -1 
    'upload_content_length' => float -1 
    'starttransfer_time' => float 0 
    'redirect_time' => float 0 
    'certinfo' => 
    array (size=0) 
     empty 
    'redirect_url' => string '' (length=0) 

我想也許是一個連接問題,但我嘗試了一個普通的http URL,curl顯示它能夠獲取頁面的內容。

更新1

我應該做的功課,然後再發布。使用Wireshark的,我可以看到下面的文字:

報警消息 等級:嚴重(2) 說明:未知CA(48)

是我設置配置不當?

回答

0

這個答案解決了這個問題對我來說: HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK

最終的解決方案:

<?php 
namespace com\voting; 

class Freebase{ 

    public function query(){ 
     $service_url = 'https://www.googleapis.com/freebase/v1/search'; 
     $params = array(
       'query' => 'Cee Lo Green', 
       'filter' => '(all type:/music/artist created:"The Lady Killer")', 
       'limit' => 10, 
       'key' => API_KEY ); 
     $url = $service_url . '?' . http_build_query($params); 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_VERBOSE, true); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     //WARNING: this would prevent curl from detecting a 'man in the middle' attack 
     curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
     curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
     $response = json_decode(curl_exec($ch), true); 
     curl_close($ch); 
     return $response; 
    } 
} 
?> 
1

感謝,在短短的腳本:

<?php 


     $service_url = 'https://www.googleapis.com/freebase/v1/search'; 
     $params = array(
       'query' => 'Cee Lo Green', 
       'filter' => '(all type:/music/artist created:"The Lady Killer")', 
       'limit' => 10, 
       'key' => API_KEY ); 
     $url = $service_url . '?' . http_build_query($params); 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_VERBOSE, true); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     //WARNING: this would prevent curl from detecting a 'man in the middle' attack 
     curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
     curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
     $response = json_decode(curl_exec($ch), true); 
     curl_close($ch); 
     return $response; 

?> 
相關問題