PHP使用file_get_content设置头信息的方法

这篇文章主要介绍了PHP使用file_get_content设置头信息的方法,结合实例形式对比分析了PHP使用fopen及file_get_contents设置及获取头信息的相关技巧,需要的朋友可以参考下。

本文实例讲述了PHP使用file_get_content设置头信息的方法,分享给大家供大家参考,具体如下:

直接上代码:

  1. <?php
  2. /**
  3. Accept application/json
  4. Accept-Encoding gzip, deflate
  5. Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
  6. Connection keep-alive
  7. Cookie mzys_is_frist_access=1; mzys_c_uuid=guest_215cbc0979b5bd2; mzys_access20130610=1; mzys_qz_uuid=d6d4640f93cb7a4; CNZZDATA30040410=cnzz_eid%3D1008690174-1370849811-http%253A%252F%252Fm.120ask.com%26ntime%3D1370855345%26cnzz_a%3D43%26retime%3D1370855484258%26sin%3D%26ltime%3D1370855484258%26rtime%3D0; PHPSESSID=4kacoqfe4ae33km3h0l2imjfd6
  8. Host m.120ask.com
  9. Referer http://m.120ask.com/health/show?page=2&id=84882&type=17
  10. User-Agent Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0
  11. X-Requested-With XMLHttpRequest
  12. */
  13. $hdrs = array(
  14. 'http' =>array('header' =>
  15. "Accept: application/json\r\n" .
  16. "Accept-Encoding: gzip, deflate\r\n" .
  17. "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n" .
  18. "Accept-Encoding: gzip, deflate\r\n" .
  19. "Connection: keep-alive\r\n" .
  20. "Cookie: mzys_is_frist_access=1; mzys_c_uu .
  21. "Host: m.120ask.com\r\n" .
  22. "Referer: http://m.120ask.com/health/show?page=2& .
  23. "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0\r\n" .
  24. "X-Requested-With: XMLHttpRequest",
  25. 'timeout'=>2
  26. ),
  27. );
  28. $context = stream_context_create($hdrs);
  29. //方式1
  30. // $fp = fopen("http://m.120ask.com/health/show?page=2&, 'r', false, $context);
  31. // fpassthru($fp);
  32. // fclose($fp);
  33. //方式2
  34. //echo file_get_contents('http://m.120ask.com/health/show?page=2&, 0, $context);