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 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试