Php代码在一台服务器上运行但在另一台服务器上运行。 为什么? [关闭]
I made a short code to calculate distance using Google Maps API. My problem is that the code works fine on one server, but not on another. It is the axact same code copied from one place to antother. I can't figure out whats wrong. Can anyone help me?
$server="https://maps.googleapis.com/maps/api/directions/json?origin=".$from.",Sweden&destination=".$to.",Sweden";
if($stream = fopen( $server,r)){
$klart=json_decode(stream_get_contents($stream),true);
fclose($stream);
$distans = $klart['routes'][0]['legs'][0]['distance'][text];
$kilometer=split(" ",$distans);
$km = $kilometer[0];
$kostnad = $km * ((2.1*1.15)*1.25)/5;
}
Thank you! Anders
Edit 1: Thank you all for your comments. It seems that it was a javascript that interfered with the html-form. I've added a script to autocomplete the cities (variables $to and $from). When I comment out the javascript, the php code works fine.
Excuse the delay in adding commentary, something urgent cropped up and I had to leave. Initially when I ran the code there were several errors thrown - aside from the lack of $from
& $to
there were two undefined constants ~ namely r
and text
$from='malmo';
$to='stockholm';
$server="https://maps.googleapis.com/maps/api/directions/json?origin=".$from.",Sweden&destination=".$to.",Sweden";
/* the mode `r` needs to be quoted */
if($stream = fopen( $server,'r')){
$klart=json_decode(stream_get_contents($stream),true);
fclose($stream);
/* the field `text` also needs to be quoted - unless it really is a constant defined elsewhere */
$distans = $klart['routes'][0]['legs'][0]['distance']['text'];
/* as `split` is now deprecated, use preg_split & split string on a space `\s` */
$kilometer=preg_split('@\s@',$distans);
$km = $kilometer[0];
$kostnad = $km * ((2.1*1.15)*1.25)/5;
echo $kostnad;
}
outputs:
369.495