gpt4 book ai didi

php - 使用 HTTPS 让 cURL 在 PHP 中工作

转载 作者:搜寻专家 更新时间:2023-10-31 21:41:18 25 4
gpt4 key购买 nike

我有以下代码可以在不使用 SSL 的测试区域中使用,但不能在使用 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 的事实有关(因为这是我的测试区域和我当前区域之间的唯一区别),但我不知道如何让它工作。

最佳答案

要确认第一个问题是否由 ssl 引起,请检查以下内容

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

它适用于那个吗?

关于php - 使用 HTTPS 让 cURL 在 PHP 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10761943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com