php模仿用户访问网页程序代码

  1. function httpget( $url, $followredirects=true ) {
  2. global $final_url;
  3. $url_parsed = parse_url($url);
  4. if ( emptyempty($url_parsed['scheme']) ) {
  5. $url_parsed = parse_url('http://'.$url);
  6. }
  7. $final_url = $url_parsed;
  8. $port = $url_parsed["port"];
  9. if ( !$port ) {
  10. $port = 80; //开源代码phpfensi.com
  11. }
  12. $rtn['url']['port'] = $port;
  13. $path = $url_parsed["path"];
  14. if ( emptyempty($path) ) {
  15. $path="/";
  16. }
  17. if ( !emptyempty($url_parsed["query"]) ) {
  18. $path .= "?".$url_parsed["query"];
  19. }
  20. $rtn['url']['path'] = $path;
  21. $host = $url_parsed["host"];
  22. $foundbody = false;
  23. $out = "get $path http/1.0 ";
  24. $out .= "host: $host ";
  25. $out .= "user-agent: mozilla/5.0 (windows; u; windows nt 5.1; en-us; rv:1.8.1) gecko/20061010 firefox/2.0 ";
  26. $out .= "connection: close ";
  27. if ( !$fp = @fsockopen($host, $port, $errno, $errstr, 30) ) {
  28. $rtn['errornumber'] = $errno;
  29. $rtn['errorstring'] = $errstr;
  30. }
  31. fwrite($fp, $out);
  32. while (!@feof($fp)) {
  33. $s = @fgets($fp, 128);
  34. if ( $s == " " ) {
  35. $foundbody = true;
  36. continue;
  37. }
  38. if ( $foundbody ) {
  39. $body .= $s;
  40. } else {
  41. if ( ($followredirects) && (stristr($s, "location:") != false) ) {
  42. $redirect = preg_replace("/location:/i", "", $s);
  43. return httpget( trim($redirect) );
  44. }
  45. $header .= $s;
  46. }
  47. }
  48. fclose($fp);
  49. return(trim($body));