如何使用curl获取距离?

use*_*643 -4 php curl

我有这个网址

http://www.worldatlas.com/travelaids/driving_distance.htm
Run Code Online (Sandbox Code Playgroud)

当我放

From city:
Hollywood, FL, United States
to city:
Washington Avenue, Miami Beach, FL, United States
Run Code Online (Sandbox Code Playgroud)

我距离 18.33mi / 29.51km

我需要一个php脚本,它将上述两个地址发送给url,作为回报,我将获得上述距离。听说使用CURL可以获取帮助/指南,

谢谢

Sud*_*oti 5

这样的事情应该起作用:


$origin = urlencode("Hollywood, FL");
$destination = urlencode("Washington Avenue, Miami Beach, FL");
$url = "http://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$destination&sensor=false";
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $url);

//return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string $output = curl_exec($ch);

// close curl resource to free up system resources curl_close($ch);
$arr = json_decode($output, TRUE); echo ""; print_r($arr); //get distance from this array

参考:Google Maps Directions希望对您有所帮助

  • @ user1133643-您的意思是? (3认同)