php 5.3.3 apache-2.2.16  windows7<?php 
$url = 'http://wwwapps.ups.com/WebTracking/track';
$data = array ('trackNums' => '11111','loc' => 'zh_CN','HTMLVersion' => '5.0','track.x' => '%E8%BF%BD%E8%B8%AA');
$data = http_build_query($data);$opts = array (
'http' => array (
    'method' => 'POST',
    'header'=> "Host: www.ups.com\r\n" .
        "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1\n\r"
        //.
        //"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n\r".
      //  "Accept-Language: zh-cn,zh;q=0.5\n\r".
     //   "Accept-Encoding: gzip, deflate\n\r".
     //   "Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\n\r".
     //   "Connection: keep-alive"
     ,
    'content' => $data
)
);
$ctx = stream_context_create($opts);
$html = @file_get_contents($url,'',$ctx);
echo $html;如果我打开注释代码   file_get_contents 抓取不到页面 请教下 为什么呢  我发送的头部信息 有错误吗   

解决方案 »

  1.   

    注意http body格式
    $data = http_build_query($data);
    要改成
    $data = html_entity_decode(http_build_query($data));
      

  2.   

    你用http_build_query实际上出来的&是&amp;,这样扔http body里是不符合http标准的。
      

  3.   

    谢谢楼上提醒  但还是没用  我header有问题吗
      

  4.   

    我这边可以抓取到页面,提示1111不是有效的追踪码。
    header有没有问题自己装个sniff或者wireshark,或者简单的firefox+firebug查看下请求头看看。