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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line