Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DanielRønfeldt's avatar

Help converting cURL request into Guzzle request

Given this cURL-driven request:

$user_id = "auth0|xxxxxxxxxxxxxxxxxxxx";
$temp_token = "xxxxxxxxxxxxxxx..........xxxxxxxxxxxxxx";

$curl = curl_init();
		
curl_setopt_array($curl, array(
	CURLOPT_URL => "https://xxxxxxx.xx.auth0.com/api/v2/users/" . $user_id,
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING => "",
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 30,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => "PATCH",
	CURLOPT_POSTFIELDS => "{ \"given_name\": $request->firstName, \"family_name\": $request->lastName }",
	CURLOPT_HTTPHEADER => array(
		"authorization: Bearer " . $temp_token,
		"cache-control: no-cache",
		"content-type: application/json"
	),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
	echo "cURL Error #:" . $err;
} else {
	echo $response;
}

I've been trying to convert it into a Guzzle-driven request, like so:

$headers = [
	"Authorization" => "Bearer " . $temp_token,
	"Cache-Control" => "no-cache",
	"Accept" => "application/json",
];

$g_uri = "https://xxxxxxx.xx.auth0.com/api/v2/users/" . $userId;

$client = new Client();
$g_request = $client->get($g_uri);

$g_request->getCurlOptions()->set(CURLOPT_RETURNTRANSFER, true);
$g_request->getCurlOptions()->set(CURLOPT_ENCODING, "");
$g_request->getCurlOptions()->set(CURLOPT_MAXREDIRS, 10);
$g_request->getCurlOptions()->set(CURLOPT_TIMEOUT, 30);
$g_request->getCurlOptions()->set(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$g_request->getCurlOptions()->set(CURLOPT_CUSTOMREQUEST, "PATCH");
$g_request->getCurlOptions()->set(CURLOPT_POSTFIELDS, "{ \"given_name\": $request->firstName, \"family_name\": $request->lastName }");
$g_request->getCurlOptions()->set(CURLOPT_HTTPHEADER, $headers);

$g_response = $g_request->send();

echo $g_response;

But it's throwing back a 401 Unauthorized exception, unlike the original cURL request. Is there a better way of handling this?

FYI: I'm using Laravel 6.x.

Many thanks.

0 likes
2 replies
ahmeddabak's avatar
Level 47

You are not sending the headers with the get request.


$client = new Client();

$response = $client->get("https://xxxxxxx.xx.auth0.com/api/v2/users/{$userId}" ,[
    'headers' => [
        "Authorization" => "Bearer {$temp_token}",
	"Cache-Control" => "no-cache",
	"Accept" => "application/json",
    ]
]);

echo $response->getBody()->getContents();
1 like
DanielRønfeldt's avatar

Alright, that seems to have solved my problem, as in, I'm not getting the 401 Unauthorized error anymore. But let me get this straight: the two strings representing the user's first and last name, they should be sent as part of the 'body' parameter of the request, like so:

$response = $client->get("https://xxxxxxx.xx.auth0.com/api/v2/users/{$userId}", [
	'headers' => [
		"Authorization" => "Bearer {$temp_token}",
		"Cache-Control" => "no-cache",
		"Accept" => "application/json",
	],
	'body' => "{ \"given_name\": $request->firstName, \"family_name\": $request->lastName }"
]);

Is my assumption correct? Because if it is, then the data is not being pushed to the Auth0 server. I'm a bit confused here...

PS: I totally forgot about including PHP variables within strings within curly braces, so thank you for helping me write a more efficient code! :)

1 like

Please or to participate in this conversation.