0

这个问题类似于:Freebase API call works in browser but failed with curl but no answers there。

我有这个在浏览器中工作的 URL:

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)

我的设置是否配置不正确?

4

2 回答 2

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;

?>
于 2014-02-15T13:12:50.827 回答
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;
    }
}
?>
于 2013-05-31T16:07:59.683 回答