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.