gpt4 book ai didi

guzzle - 将 cURL 请求翻译为 Guzzle

转载 作者:行者123 更新时间:2023-12-03 15:55:32 31 4
gpt4 key购买 nike

我正在尝试使用 Guzzle 而不是直接使用 cURL 来实现和 HTTP 请求。我如何使用 Guzzle 发出相同类型的请求?或者我应该坚持使用cURL?

$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// don't verify SSL certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Return the contents of the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Set up authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$token:X");

这是我的方法。我一直遇到 401 Unauthorized 错误。我知道我有正确的凭据。让我觉得我没有走上正轨的是 Guzzle 文档指出:目前仅在使用 cURL 处理程序时支持 auth,但计划创建一个可以与任何 HTTP 处理程序一起使用的替代品。但根据我的理解,Guzzle 默认使用 cURL。
$guzzleData = [
'auth' => [$token, 'X'],
'allow_redirects' => true,
'verify' => false,
];

$client = new \Guzzle\Http\Client();
$request = $client->get($url, $guzzleData);
$response = $request->send();

最佳答案

这是解决方案:

$client = new \Guzzle\Http\Client();
$request = $client->get($url);

$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
$request->getCurlOptions()->set(CURLOPT_RETURNTRANSFER, true);
$request->getCurlOptions()->set(CURLOPT_FOLLOWLOCATION, true);
$request->getCurlOptions()->set(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$request->getCurlOptions()->set(CURLOPT_USERPWD, "$token:X");

$response = $request->send();

关于guzzle - 将 cURL 请求翻译为 Guzzle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31752981/

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