有人可以帮我解决有关 Laravel 中 Guzzle 的问题吗?
我有一段使用curl 的代码,它可以工作。
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, ['userId' => $appUserId]);
curl_setopt($ch, CURLOPT_URL, $url);
$content = curl_exec($ch);
var_dump($content); // I get a response, response is good.
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
我得到了一个很好的旧回复,其中包含我需要的所有数据。简单又美好。
然而,我工作的公司在整个应用程序中都使用 Guzzle,所以当我这样使用 Guzzle 时。
$client = new GuzzleHttp\Client();
try {
$res = $client->request('POST', $url, [
'form_params' => [
'userId' => $appUserId
],
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
// I have tried every imaginable header.
]);
$result = json_decode($res->getBody());
echo $res->getBody();
var_dump($result);
} catch (\GuzzleHttp\Exception\RequestException $e) {
var_dump('error');
$guzzleResult = $e->getResponse();
var_dump($guzzleResult);
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我遇到了重大错误。
这是错误。
object(GuzzleHttp\Psr7\Response)#317 (6) {
["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
string(12) "Unauthorized"
["statusCode":"GuzzleHttp\Psr7\Response":private]=>
int(401)
["headers":"GuzzleHttp\Psr7\Response":private]=>
array(7) {
["Host"]=>
array(1) {
[0]=>
string(14) "127.0.0.1:8000"
}
["Date"]=>
array(2) {
[0]=>
string(29) "Wed, 19 Aug 2020 13:39:56 GMT"
[1]=>
string(29) "Wed, 19 Aug 2020 13:39:56 GMT"
}
["Connection"]=>
array(1) {
[0]=>
string(5) "close"
}
["X-Powered-By"]=>
array(1) {
[0]=>
string(9) "PHP/7.3.6"
}
["Cache-Control"]=>
array(1) {
[0]=>
string(17) "no-cache, private"
}
["Content-Type"]=>
array(1) {
[0]=>
string(16) "application/json"
}
["Set-Cookie"]=>
array(2) {
[0]=>
string(316) "XSRF-TOKEN=eyJpdiI6ImlPSHczMXRUQ0ZZTUVpRkFPZ052UHc9PSIsInZhbHVlIjoiS0UrYVdkR2ZETWhcL2FLVjRhSVZPM1VxYjVWb3ZoN2dTZlwvWFlNa2hXam1WNVo1NHo3Mk9aZXc5ZFlzYzhlTlNUIiwibWFjIjoiYThiYWVlYjRmOWJmMDgxOWJmZDQzYjg2YWFmZDdjMDFiYzg0ODk5N2FlZTk3ZmI0YjY5MzlmNzYzMGExMThlYSJ9; expires=Wed, 19-Aug-2020 15:39:56 GMT; Max-Age=7200; path=/"
[1]=>
string(331) "laravel_session=eyJpdiI6ImpkK2ZtblU3MFpBN3hoSlJ1Wk5Nb2c9PSIsInZhbHVlIjoiVVYxeGtTV09ZZzVoMUcyXC8yaVI0XC9ZbWdYa0hQdkxQZ0xMamE1YzNaZ0NCTjJYbWJSUGl0MWMwRU9rczFUeHdQIiwibWFjIjoiNjJkMGVkYmI0ZDcwMTg4YmZmNjZmYjliYzFjOTI1ODRlN2I4ZTM1MjYyN2U0M2M0YzlmODk0YWVjYzRhNzAyOCJ9; expires=Wed, 19-Aug-2020 15:39:56 GMT; Max-Age=7200; path=/; httponly"
}
}
["headerNames":"GuzzleHttp\Psr7\Response":private]=>
array(7) {
["host"]=>
string(4) "Host"
["date"]=>
string(4) "Date"
["connection"]=>
string(10) "Connection"
["x-powered-by"]=>
string(12) "X-Powered-By"
["cache-control"]=>
string(13) "Cache-Control"
["content-type"]=>
string(12) "Content-Type"
["set-cookie"]=>
string(10) "Set-Cookie"
}
["protocol":"GuzzleHttp\Psr7\Response":private]=>
string(3) "1.1"
["stream":"GuzzleHttp\Psr7\Response":private]=>
object(GuzzleHttp\Psr7\Stream)#315 (7) {
["stream":"GuzzleHttp\Psr7\Stream":private]=>
resource(484) of type (stream)
["size":"GuzzleHttp\Psr7\Stream":private]=>
int(49)
["seekable":"GuzzleHttp\Psr7\Stream":private]=>
bool(true)
["readable":"GuzzleHttp\Psr7\Stream":private]=>
bool(true)
["writable":"GuzzleHttp\Psr7\Stream":private]=>
bool(true)
["uri":"GuzzleHttp\Psr7\Stream":private]=>
string(10) "php://temp"
["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
array(0) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将调试设置为 true,我会得到这个
ErrorException:curl_setopt_array():无法将输出类型的流表示为文件C:\ xampp-7 \ htdocs \ website \vendor \ guzzlehttp \ guzzle \ src \ Handler \ CurlFactory.php第59行中的STDIO FILE *
我以前用过Guzzle,应该相当简单。
'debug' => fopen('php://stderr', 'w'),
Run Code Online (Sandbox Code Playgroud)
这对我有用
$client->request('POST', $url, [
'debug' => fopen('php://stderr', 'w'),
'form_params' => [
'userId' => $appUserId
],
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json'
],
]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7611 次 |
| 最近记录: |