Freebase API在浏览器中运行,但在PHP中不运行

时间:2013-05-31 15:55:22

标签: php freebase

此问题类似于:Freebase API call works in browser but fails with curl,但没有答案。

我在浏览器中使用此网址:

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网址,并且卷曲显示它能够获取网页的内容。

更新1

我应该在发布之前完成我的作业。使用Wireshark,我可以看到以下文字:

警报讯息 等级:致命(2) 描述:未知CA(48)

我的设置配置不正确吗?

2 个答案:

答案 0 :(得分: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;

?>

答案 1 :(得分: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;
    }
}
?>