Not getting any response using PHP (CURL)
Hi I literally tried everything but I can’t get any response. When I file same request using postman it works. No idea what’s wrong. I hide rapidapi key for this sample, in original file there is rapidapi key. I get 200 status code, api call is not registered on rapidapi.
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
class Route
{
public $t;
}
$firstRoute = new Route();
$firstRoute->t = "Boston";
$secondRoute = new Route();
$secondRoute->t = "New York";
$route_php = array($firstRoute, $secondRoute);
$route_json = json_encode($route_php);
$route = "$first_destination, $second_destination";
$url = `https://distanceto.p.rapidapi.com/get?route=$route_json&car=false&foot=false`;
$curl = curl_init($url);
$headers = array(
"x-rapidapi-host: distanceto.p.rapidapi.com",
"x-rapidapi-key: HIDDEN",
'Accept: application/json',
'Content-Type: application/json'
);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_ENCODING, "");
$response = curl_exec($curl);
echo $response;
$phpObj = json_decode($response);
echo $phpObj;
curl_close($curl);
?>```
加入讨论 - 在下面添加评论:
Thank you so much for fast response, I tried it and it worked. I think you are right urlencode may fix that. Thanks again! // tried it now with urlencode and it works nice
Did you try out the example Code Snippet from https://rapidapi.com/Distance.to/api/distance ?
`
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => “https://distanceto.p.rapidapi.com/get?route=[{“t”%3A"TXL"}%2C{“t”%3A"Hamburg"}]&car=false&foot=false”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “GET”,
CURLOPT_HTTPHEADER => [
“x-rapidapi-host: distanceto.p.rapidapi.com”,
“x-rapidapi-key: <YOUR KEY>”
],
]);
response=curlexec(curl);
err=curlerror(curl);
curl_close($curl);
if ($err) {
echo “cURL Error #:” . $err;
} else {
echo $response;
}
`
I think you forgot to urlencode $route_json.