[英]Getting cURL to work in PHP with HTTPS
I have the following code which works in test regions which do not use SSL, but not on the production system, which does. 我有以下代码,它们可以在不使用SSL的测试区域中工作,但不能在生产系统上工作。 The call itself works in a browser, but when running it via cURL, I get a 500 error.
调用本身在浏览器中工作,但是当通过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;
If I don't put in CURL_OPT_FAILONERROR, then I don't get any error, just a blank response. 如果我没有输入CURL_OPT_FAILONERROR,那么我不会收到任何错误,只是一个空白的响应。 I'm pretty sure this has to do with the fact that this is via https (as that is the only difference between my test region and my current region), but I can't figure out how to get this to work.
我很确定这与通过https(因为这是我的测试区域和我当前区域之间的唯一区别)这一事实有关,但我无法弄清楚如何使其工作。
To confirm the first issue is due to ssl or not, check with following 要确认第一个问题是由ssl引起的,请查看以下内容
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Does it work with that ? 这有用吗?
From what you described, most likely the issue is with matching the certificate. 根据您的描述,问题很可能与匹配证书有关。 Try:
尝试:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
Obviously this isn't a "solution" to your problem, but it will help you in troubleshooting. 显然这不是解决问题的“解决方案”,但它可以帮助您排除故障。
Best of luck! 祝你好运!
Try something like 尝试类似的东西
<?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;
}
And then download the certificate from the website you are accessing (Open firefox, go to https://site.com , right click, view page info, security tab, certificate, select topmost cert, export as x.509 and then use update the CURLOPT_CAINFO
然后从您正在访问的网站下载证书(打开Firefox,转到https://site.com ,右键单击,查看页面信息,安全选项卡,证书,选择最顶层的证书,导出为x.509,然后使用更新
CURLOPT_CAINFO
Hope this helps, Dave 希望这有帮助,戴夫
Try to set CURLOPT_REFERER and CURLOPT_USERAGENT. 尝试设置CURLOPT_REFERER和CURLOPT_USERAGENT。 I had the same problem and this solved it.
我有同样的问题,这解决了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.