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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大