在功能不起作用中两次发送curl请求

时间:2014-12-23 12:12:04

标签: php curl dictionary

我发送curl请求到freemaptools.com以查找半径中的邮政编码。我的代码在下面

function get_zip_in_radius($lng,$lat,$radius,$a) {
    $headers = array(
                     'Referer:http://www.freemaptools.com/find-zip-codes-inside-radius.htm',
                     'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36',
                     'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                     'Accept-Language:en-US,en;q=0.5',
                     'Connection:keep-alive',
                     'Pragma:no-cache',
                     'Cache-Control:no-cache',
                     'Origin:http://www.freemaptools.com',
                     'Content-Type:text/plain; charset=UTF-8'    
                     );

    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, 'http://www.freemaptools.com/ajax/get-all-zip-codes-inside.php?mode=9&radius='.$radius.'&lat='.$lat.'&lng='.$lng.'&rn='.$a);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_handle, CURLOPT_HEADER, 1); 
    curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl_handle, CURLOPT_COOKIE, "__utma=126142042.1539287389.1412828292.1412828292.1412828292.1; __utmb=126142042.1.10.1412828292; __utmc=126142042; __utmz=126142042.1412828292.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1");

    $buffer = curl_exec($curl_handle);

    $header_size = curl_getinfo($curl_handle, CURLINFO_HEADER_SIZE);
    $body = substr($buffer, $header_size);

    curl_close($curl_handle);

    $body = strtr($body,array("<"=>"&lt;","&"=>"&amp;")); // for     displaying html tags

    function get_string($string, $start, $end){
        $found = array();
        $pos = 0;
        while( true )
            {
                $pos = strpos($string, $start, $pos);
                if ($pos === false) { // Zero is not exactly equal to false...
                    return $found;
                }
                $pos += strlen($start);
                $len = strpos($string, $end, $pos) - $pos;
                $found[] = substr($string, $pos, $len);
            }
    }
    return get_string($body,'zipcode="','"');
}

function get_zip()
{    $post=$this->get_zip_in_radius($value[0]->geometry->location->lng,$value[0]->geometry->location->lat,'50','7623');
    $post2=$this->get_zip_in_radius($value[0]->geometry->location->lng,$value[0]->geometry->location->lat,'100','6542');
}

现在我的问题是当我发送请求两个半径为50KM而另一个为100KM时,只有一个请求发送给我其他的请求不是。任何人都可以告诉我我的代码中可能存在什么问题。请帮助我。感谢您的帮助。谢谢。

来自Google API的$value[0]->geometry->location->lng$value[0]->geometry->location->lat的价值

1 个答案:

答案 0 :(得分:0)

function get_string($string, $start, $end)
{
    $found = array();
    $pos = 0;
    while( true )
        {
            $pos = strpos($string, $start, $pos);
            if ($pos === false) { // Zero is not exactly equal to false...
            return $found;
        }
            $pos += strlen($start);
            $len = strpos($string, $end, $pos) - $pos;
            $found[] = substr($string, $pos, $len);
        }
     return $found;
}

function get_zip_in_radius($lng,$lat,$radius,$a) {
$headers = array(
                 'Referer:http://www.freemaptools.com/find-zip-codes-inside-radius.htm',
                 'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36',
                 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                 'Accept-Language:en-US,en;q=0.5',
                 'Connection:keep-alive',
                 'Pragma:no-cache',
                 'Cache-Control:no-cache',
                 'Origin:http://www.freemaptools.com',
                 'Content-Type:text/plain; charset=UTF-8'    
                 );

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'http://www.freemaptools.com/ajax/get-all-zip-codes-inside.php?mode=9&radius='.$radius.'&lat='.$lat.'&lng='.$lng.'&rn='.$a);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_handle, CURLOPT_HEADER, 1); 
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_COOKIE, "__utma=126142042.1539287389.1412828292.1412828292.1412828292.1; __utmb=126142042.1.10.1412828292; __utmc=126142042; __utmz=126142042.1412828292.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1");

$buffer = curl_exec($curl_handle);

$header_size = curl_getinfo($curl_handle, CURLINFO_HEADER_SIZE);
$body = substr($buffer, $header_size);

curl_close($curl_handle);

$body = strtr($body,array("<"=>"&lt;","&"=>"&amp;")); // for     displaying html tags


return get_string($body,'zipcode="','"');
}

function get_zip()
{        $post=$this->get_zip_in_radius($value[0]->geometry->location->lng,$value[0]->geometry->location->lat,'50','7623');
    $post2=$this->get_zip_in_radius($value[0]->geometry->location->lng,$value[0]->geometry->location->lat,'100','6542');
}

get_string()get_zip_in_radius()之后定义其工作正常。