我有以下代码,它可以在不使用SSL的测试区域中工作,但不能在生产系统上工作。调用本身可以在浏览器中运行,但是当通过cURL运行它时,我得到了一个500错误。
$region = "https://api.mysite.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . "/cacert.pem");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_URL, $region . $api);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$resp = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
return $resp;
如果我不输入CURL_OPT_FAILONERROR,那么我不会得到任何错误,只会得到一个空白的响应。我非常确定这与这是通过https (因为这是我的测试区域和我的当前区域之间的唯一区别)的事实有关,但我不知道如何让它工作。
发布于 2012-06-01 10:25:49
要确认第一个问题是否是由ssl引起的,请与以下人员联系
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
它能与此一起工作吗?
发布于 2012-05-25 22:28:14
根据您所描述的,问题最有可能是与证书匹配。尝试:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
显然,这不是您问题的“解决方案”,但它将帮助您进行故障排除。
祝你好运!
发布于 2012-05-25 23:06:36
试试像这样的东西
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$data = array(
CURLOPT_CAINFO => "/Users/davidmann/Documents/facebook.pem",
CURLOPT_SSL_VERIFYHOST => 1,
CURLOPT_SSL_VERIFYPEER => true
);
echo var_dump(curl_get('https://www.facebook.com/', array('Refferer' => 'https://www.facebook.com/'), $data));
/**
* Send a GET requst using cURL
* @param string $url to request
* @param array $get values to send
* @param array $options for cURL
* @return string
* @author David from Code2Design.com
* @link http://au.php.net/manual/en/function.curl-exec.php#98628
*/
function curl_get($url, array $get = NULL, array $options = array())
{
$defaults = array(
CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get),
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 4
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
然后从您要访问的网站下载证书(打开firefox,转到https://site.com,右键单击,查看页面信息,安全选项卡,证书,选择最顶层的证书,导出为x.509,然后使用更新CURLOPT_CAINFO
希望这对你有帮助,戴夫
https://stackoverflow.com/questions/10761943
复制相似问题