duanhuizhe6767 2016-02-02 14:40
浏览 45
已采纳

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.

  • 写回答

1条回答 默认 最新

  • doujie7346 2016-02-02 14:47
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?