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