Reputation: 95
I'm trying to use a variable as URL in a cURL GET request but it's not working.
$data is sent via ajax from the other page. If I echo $data once it's been declared and before $curl is declared, the URL I want to use appears (https://api.calendly.com/event_types/d5d0d786-54b8-4e85-83ce-5b5d853957fa
).
But using $data in the cURL request just won't work. If I replace the $data variable with the URL as a string - "https://api.calendly.com/event_types/d5d0d786-54b8-4e85-83ce-5b5d853957fa"
, it works fine and returns the value I want from the response ('scheduling_url'), so I can't see why the $data variable that contains the same thing won't work.
Is the variable somehow not being passed properly into the cURL request? Any advice greatly appreciated. Thanks.
<?php
$data = isset($_REQUEST['myData'])?$_REQUEST['myData']:"";
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer MY_TOKEN",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$response_json = json_decode($response, true);
echo $response_json['resource']['scheduling_url'];
}
?>
Upvotes: 1
Views: 1334
Reputation: 95
Solved it - turned out there was some additional white space at the end of the stored $data URL which I cleaned using trim().
Upvotes: 2