【问题标题】:php_curl not able to run https (Google+ API)php_curl 无法运行 https (Google+ API)
【发布时间】:2015-05-02 08:39:27
【问题描述】:

我正在尝试使用以下代码从 Google+ 中提取分享数:

function get_gplus_shares($url){
    $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
      curl_setopt($curl, CURLOPT_POST, 1);
      curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
      $curl_results = curl_exec ($curl);
      curl_close ($curl);
      $json = json_decode($curl_results, true);
      return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
}

它在我的本地测试环境中运行良好,但在我的实际环境中无法运行。我已经使用sudo apt-get install curl 和sudo apt-get install php5-curl 安装了php_curl,工作正常。还通过sudo wget http://curl.haxx.se/ca/cacert.pem -O /usr/lib/ssl/certs/cacert.pem 添加了cacert.pem。但似乎不起作用。我真的不知道从哪里开始。

错误消息 - 来自 apache 错误日志。

RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?) - 但在添加 cacert.pem 后,此错误似乎已修复。

PHP Notice: Undefined index: result - PHP错误,函数结果为空。

[ssl:error] [pid 20743] AH02032: Hostname **IP-ADDRESS** provided via SNI and hostname **mydomain.com** provided via HTTP are different - 刚刚看到这个错误,不确定是否相关。

【问题讨论】:

  • 请贴出所有错误信息
  • 请参阅编辑@AxelAmthor。
  • 仅供参考,这是一个私有的 Google API,可能随时更改或中断,恕不另行通知。

标签: php ssl curl google-plus


【解决方案1】:

您能否通过添加以下两个选项来检查

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
curl_setopt ($ch, CURLOPT_CAINFO, "pathto/cacert.pem");

【讨论】:

  • 仍然没有运行,Undefined index: result 错误。
【解决方案2】:

检查以下是否有效:

<?PHP

// connect via SSL, but don't check cert
$handle=curl_init('https://www.google.com');
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$content = curl_exec($handle);

echo $c
?>

【讨论】:

  • 输出是:302 Moved The document has moved here.这里是https://www.google.com.sg/?gfe_rd=cr&amp;ei=iZtEVfaaNYPW8gewnoCoBw
  • 您必须更改自己的 URL,为此使用“google.co.uk/?gfe_rd=cr&ei=LpxEVbm3FsGq8wfXqIHgDw”而不是“google.com”
  • 亲爱的,您必须添加帖子中给出的选项和帖子参数,并更改网址
  • 知道了,伙计!问题出在网址上,我正在传递编码的网址。猜猜这就是问题所在。感谢您的帮助!
猜你喜欢
  • 2011-02-09
  • 2012-03-13
  • 2014-05-15
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
  • 2017-09-23
相关资源
最近更新 更多