Distance

부분 유료
분류별 Distance.to | 업데이트됨 2 месяца назад | Mapping
인기

9.6 / 10

지연 시간

414ms

서비스 수준

100%

Health Check

100%

모든 토론으로 돌아가기

Not getting any response using PHP (CURL)

Rapid account: Oficjalne 998
oficjalne998
4 года назад

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 4 года назад

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 4 года назад

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.

아래에 의견을 추가하고 토론에 참여하세요.

새 댓글을 게시하려면 로그인 / 가입