fork download
  1. <?php
  2. class Request
  3. {
  4. var $_fp;
  5. var $_url;
  6. var $_host;
  7. var $_protocol;
  8. var $_uri;
  9. var $_port;
  10.  
  11. private function _scan_url()
  12. {
  13. $req = $this->_url;
  14. $pos = strpos($req, '://');
  15. $this->_protocol = strtolower(substr($req, 0, $pos));
  16. $req = substr($req, $pos+3);
  17. $pos = strpos($req, '/');
  18. if($pos === false)
  19. {
  20. $pos = strlen($req);
  21. }
  22. $host = substr($req, 0, $pos);
  23. if(strpos($host, ':') !== false)
  24. {
  25. list($this->_host, $this->_port) = explode(':', $host);
  26. }
  27. else
  28. {
  29. $this->_host = $host;
  30. $this->_port = ($this->_protocol == 'https') ? 443 : 80;
  31. }
  32. $this->_uri = substr($req, $pos);
  33. if($this->_uri == '')
  34. {
  35. $this->_uri = '/';
  36. }
  37. }
  38.  
  39. //constructor
  40. function Request($url)
  41. {
  42. $this->_url = $url;
  43. $this->_scan_url();
  44. }
  45.  
  46. //download URL to string
  47. function DownloadToString()
  48. {
  49. $crlf = "\r\n";
  50. //generate request
  51. $response = '';
  52. $req = 'GET '.$this->_uri.' HTTP/1.0'.$crlf. 'Host: '.$this->_host.$crlf.
  53. 'User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7'.$crlf.$crlf;
  54. $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
  55. fwrite($this->_fp, $req);
  56. while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
  57. {
  58. $response .= fread($this->_fp, 1024);
  59. }
  60. fclose($this->_fp);
  61. //split header and body
  62. $pos = strpos($response, $crlf.$crlf);
  63. if($pos === false)
  64. {
  65. return $response;
  66. }
  67. $header = substr($response, 0, $pos);
  68. $body = substr($response, $pos + 2*strlen($crlf));
  69. //parse headers
  70. $headers = array();
  71. $lines = explode($crlf, $header);
  72. foreach($lines as $line)
  73. {
  74. if(($pos = strpos($line, ':')) !== false)
  75. {
  76. $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));
  77. }
  78. }
  79. //redirection
  80. if(isset($headers['location']))
  81. {
  82. $http = new HTTPRequest($headers['location']);
  83. return($http->DownloadToString($http));
  84. }
  85. return $body;
  86. }
  87.  
  88. function DownloadHeadersOnly()
  89. {
  90. $crlf = "\r\n";
  91. //generate request
  92. $response = '';
  93. $req = 'GET '.$this->_uri.' HTTP/1.0'.$crlf.'Host: '.$this->_host.$crlf.$crlf;
  94. $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
  95. fwrite($this->_fp, $req);
  96. while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
  97. {
  98. $response .= fread($this->_fp, 1024);
  99. }
  100. fclose($this->_fp);
  101. //split header and body
  102. $pos = strpos($response, $crlf.$crlf);
  103. if($pos === false)
  104. {
  105. return $response;
  106. }
  107. $header = substr($response, 0, $pos);
  108. $body = substr($response, $pos + 2*strlen($crlf));
  109. $headers = array();
  110. $lines = explode($crlf, $header);
  111. foreach($lines as $line)
  112. {
  113. if(($pos = strpos($line, ':')) !== false)
  114. {
  115. $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));
  116. }
  117. }
  118. return $headers;
  119. }
  120. }
  121. ?>
Success #stdin #stdout 0.02s 13064KB
stdin
Standard input is empty
stdout

Standard output is empty