我正在尝试使用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 未经授权的错误。我知道我有正确的凭据。让我觉得我没有走在正确的道路上的是 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();