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

时间:2012-04-20 05:36:29

标签: php html curl

$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

在行echo $address;中将打印在页面中的内容中,但如果我取消注释下面的代码(以$token=strtok开头的代码),$ address将显示为空。我添加了更多使用$ address结果的代码(但我没有在此处包含它),有时会出现在curl初始化中调用的站点的源代码。所以我想也许curl会花一点时间,但我试着在srtok之前睡一觉,但是没有用。

3 个答案:

答案 0 :(得分:2)

while循环中,您必须将$token = strtok(",");替换为$token = strtok($address, ",");

答案 1 :(得分:0)

由于你想要从谷歌获取MAP信息,我认为你使用错误的approch我使用javascript,因为这可能更难以解析

为什么不尝试直接使用PHP

$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']);
        }
    }
}

输出

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 :(得分:0)

将您的while循环更改为:while ($token !== false)