douyanpeng0748 2012-04-20 05:36
浏览 34
已采纳

保存在变量PHP中后无法处理卷曲响应

$ch = curl_init("http://acrs.bboxpr.com/getAddress.php?lat=35.545112&lng=-90.657635");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$address = curl_exec($ch);       
curl_close($ch);

//prints the address
echo $address;

//$token = strtok($address, ",");
//$phaddress=array();
//while ($token != null)
//{
//array_push($phaddress,$token);
//$token = strtok(",");
//}
//print_r($phaddress); //blank

In the line echo $address; will print in the content in the page, but if I uncomment the code below(the one that starts with: $token=strtok),$address will look empty. I added more code that uses the results with $address (but I did not include that in here) and sometimes appears the source-code of the site that is invoked in the curl initialization. So I think maybe curl is taking a little bit longer, but I tried to put a sleep before srtok, but didn't work.

  • 写回答

3条回答 默认 最新

  • duandaodao6951 2012-04-20 07:21
    关注

    Since you are tying to get MAP information from google i think you are using the wrong approch my using javascript because that might be more difficult to parse

    Why don't you try using PHP directly

    $ch = curl_init("http://maps.google.com/maps/api/geocode/json?latlng=35.545112,-90.657635&sensor=false");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $return = curl_exec($ch);
    curl_close($ch);
    $geoOutput = json_decode($return,true);
    echo "<pre>" ;
    
    foreach($geoOutput as $key => $data)
    {
        if(is_array($data))
        {
            foreach($data as $cKey => $cData)
            {
                var_dump($cData['formatted_address']);
            }
        }
    }
    

    Output

    string(43) "6724-6916 Bay Ln, Harrisburg, AR 72432, USA"
    string(16) "Bolivar, AR, USA"
    string(25) "Harrisburg, AR 72432, USA"
    string(17) "Poinsett, AR, USA"
    string(13) "Arkansas, USA"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计