Reputation: 560
Could anybody help me with an issue regarding Guzzle in Laravel?
I have this bit of code using curl which works.
$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);
I get a good old response with all the data I need. Simple and nice.
However, the company I work with uses Guzzle throughout the app, so when I use Guzzle as such.
$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);
}
I get major errors for some reason.
This is error.
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) {
}
}
}
If I set the debug to true, I get this
ErrorException: curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE* in file C:\xampp-7\htdocs\website\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 59
I have used Guzzle before and it should be fairly simple.
Upvotes: 0
Views: 8690
Reputation: 8499
'debug' => fopen('php://stderr', 'w'),
this worked for me
$client->request('POST', $url, [
'debug' => fopen('php://stderr', 'w'),
'form_params' => [
'userId' => $appUserId
],
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json'
],
]);
Upvotes: 5
Reputation: 10166
You are passing some form_params
to guzzle, but you are also setting the content-type to application/json
and your data is not in json format. Try with
$res = $client->request('POST', $url, [
'form_params' => [
'userId' => $appUserId
],
'headers' => [
'Accept' => 'application/json'
],
]);
Also, to make sure the response body is treated as a string, you should use getContents()
$responseData = $res->getBody()->getContents();
$result = json_decode($responseData);
Upvotes: 2