以编码形式获取卷曲响应

时间:2014-10-09 05:54:58

标签: php curl

我正在使用curl将Zipcode放在半径内我为此写了curl并且它在Shell中工作正常但是当我在PHP中转换它时我得到编码响应。我的代码在

之下

php代码

$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-Encoding:gzip, deflate',
    '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?radius=50&lat=36.7017377&lng=-95.87759640000002&rn=8523');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_connection, 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);


curl_close($curl_handle);


echo '<pre>'; print_r($buffer);

响应有点像

‹Å—OoÓ@Å¿J”;aÿÍήÔVZÚmÕõÛinT¨ZDÃ>=“TY«JØÊ%ÎÁ¿Ì{3og¾ÝüørûãñäèéËè÷Ý÷ŸnǵÖãÑ×›õñXãDXé•¡çûÏÇã7&AJ?}¼[ÿ:Ÿ†2®èááçýæñ2•Í4]ÆŠ~{\߬é}õx´¾ûvûûá~óöñèíËLØÊ*#2ÓN$"*ÈÌYX4añ~Z0ÿmO020§+¼Ó38ŸÇÕõ¼(â%oKÜ‘Áä2£TÕŽ|C5/§ƒPY½R²šQ½qÝŽ:סC@x©B9ËÜÂ[ÈUÎS>Ä!¬µ\ KX©OSA / VO«X-Aò6Ön)IE(é6³ô“lVUê-äi'dXÅμE&GT;7ðÆÏ|e|:LcÆ¥“ÖÏftêO“ʽц¡ JV-M †§©(æåsS> EQ‡½©,。¼Ò[ HÛÅè-zÊ!zH1GiJ;êšœ|ڜœ,·LËÐì_|gHmÝ2•4-rÉW|ËÔÜPM‡/ hmk>è&lt;æ,...kìtn”@œç¤u¹Æ“-á ÌúªØØXtÞØ®'èsóìý€$μ,&gt;&lt;ø&lt; =Ë| i?äáN,æNJÓq...õ7cS'| \Ùül2; QHk ÷'ö£sè'uÍ6•ƒbø‰B;,lyôœŸyWa9[ÔL؃-ˬ°hyïÐDjowÄ*Ðö5'Û¤Ùå:Ùº³©%y $ëØéU^¼À6)ÈZVy-<çz‹RdykZ®«UÏ:S´RJ¯eV¶ }úNˆcÂÒM¡»~9:BÚt½ŽÕ Ý㘦žîHºV¢Î»eÙ×BÇvÛÍ=ж8ê^GqžÃn¹*;ò÷‰|û|<ùÃr6…j

请帮助我的代码有什么问题,我们将非常感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

添加

curl_setopt($curl_handle, CURLOPT_ENCODING, "gzip");

并将其设置为0,这样您就不会获得http标头

curl_setopt($curl_handle, CURLOPT_HEADER, 0);

您告诉服务器您同意在Accept-Encoding:gzip,

的情况下接收gzip压缩数据

结果将是响应的HTTP头(如果CURLOPT_HEADER = 0则不存在)+ XML

<markers><marker zipcode="67333" lat="37.061924" lng="-95.875119" 
city="CANEY" county="MONTGOMERY" state="KS" timezone="6" /> and so on

当然你的浏览器不会显示它。制作echo hmltentities($buffer);(在这种情况下不需要预先制作),你会看到它。

答案 1 :(得分:1)

您将获得与shell脚本相同的输出:

$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?radius=50&lat=36.7017377&lng=-95.87759640000002&rn=8523');
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
echo($body);

在您的代码中,您没有关闭&#34; pre&#34;标签在那时,也是,

$curl_connection

应该是

$curl_handle