gpt4 book ai didi

php - exec ('curl -k ...' 的 curl_setopt 等价物是什么)

转载 作者:行者123 更新时间:2023-12-05 02:24:21 30 4
gpt4 key购买 nike

我正在尝试复制此命令的功能:

exec('curl -k -d "pameters=here" https://apiurlhere.com', $output);

使用 curl_exec():

$url =  'https://apiurlhere.com?parameters=here';
$ci = curl_init();
curl_setopt($ci, CURLOPT_POST, TRUE);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ci, CURLOPT_URL, $url);
$response = curl_exec($ci);
curl_close ($ci);
print_r($response);

我一直在尝试在文档 (http://php.net/manual/en/function.curl-setopt.php) 中找到的不同选项,并认为它是 CURLOPT_SSL_VERIFYPEER,但它仍然失败(响应返回空)。

我发布的 URL 使用 SNI,当 SSL 证书返回与预期不同的主机名时,这会导致 cURL 失败。

是否有与 -k 等效的 curl_setopt?

最佳答案

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE) 相当于命令行上的 -k (你不应该真正使用它,因为它建立了连接容易受到 MITM 攻击)。

这里的问题是您假设 -d "pameters=here" 等同于将这些参数放入 URL 查询中,例如 https://apiurlhere.com?参数=此处

事实并非如此,请参阅 -d 的文档:

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.

你应该为此使用 POSTFIELDS:

curl_setopt($ch, CURLOPT_POSTFIELDS, array("parameters" => "here"));

关于php - exec ('curl -k ...' 的 curl_setopt 等价物是什么),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12942297/

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