Distance

फ्रीमियम
द्वारा Distance.to | अपडेट किया गया 17 days ago | Mapping
लोकप्रियता

9.6 / 10

लेटेंसी

707ms

सेवा का स्तर

100%

Health Check

100%

सभी चर्चाओं पर वापस जाएं

Not getting any response using PHP (CURL)

Rapid account: Oficjalne 998
oficjalne998
3 years ago

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);

?>```

Rapid account: Oficjalne 998
oficjalne998 Commented 3 years ago

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

Rapid account: Distance To
Distance.to Commented 3 years ago

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(response = curl_exec(curl);
err=curlerror(err = curl_error(curl);

curl_close($curl);

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

I think you forgot to urlencode $route_json.

चर्चा में शामिल हों - नीचे टिप्पणी जोड़ें:

नई टिप्पणियाँ पोस्ट करने के लिए लॉग इन / साइनअप करें