一個讀取遠端檔案的PHP函數

WBOY
發布: 2016-07-25 08:43:53
原創
1002 人瀏覽過

一个读取远程文件的函数,非常好用!

  1. function urlfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE , $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE') {
  2. $return = '';
  3. $matches = parse_url($url);
  4. $host = $matches['host'];
  5. $path = $matches['path'] ? $matches['path'].(isset($matches['query']) ? '?'.$matches['query'] : '') : '/';
  6. $port = !empty($matches['port']) ? $matches['port'] : 80;
  7. if($post) {
  8. $out = "POST $path HTTP/1.0rn";
  9. $out .= "Accept: */*rn";
  10. $out .= "Accept-Language: zh-cnrn";
  11. $boundary = $encodetype == 'URLENCODE' ? '' : ';'.substr($post, 0, trim(strpos($post, "n")));
  12. $out .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencodedrn" : "Content-Type: multipart/form-data$boundaryrn";
  13. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]rn";
  14. $out .= "Host: $hostrn";
  15. $out .= 'Content-Length: '.strlen($post)."rn";
  16. $out .= "Connection: Closern";
  17. $out .= "Cache-Control: no-cachern";
  18. $out .= "Cookie: $cookiernrn";
  19. $out .= $post;
  20. } else {
  21. $out = "GET $path HTTP/1.0rn";
  22. $out .= "Accept: */*rn";
  23. $out .= "Accept-Language: zh-cnrn";
  24. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]rn";
  25. $out .= "Host: $hostrn";
  26. $out .= "Referer: rn";
  27. $out .= "Connection: Closern";
  28. $out .= "Cookie: $cookiernrn";
  29. }
  30. $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  31. if(!$fp) {
  32. return '';
  33. } else {
  34. stream_set_blocking($fp, $block);
  35. stream_set_timeout($fp, $timeout);
  36. @fwrite($fp, $out);
  37. $status = stream_get_meta_data($fp);
  38. if(!$status['timed_out']) {
  39. while (!feof($fp)) {
  40. if(($header = @fgets($fp)) && ($header == "rn" || $header == "n")) {
  41. break;
  42. }
  43. }
  44. $stop = false;
  45. while(!feof($fp) && !$stop) {
  46. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  47. $return .= $data;
  48. if($limit) {
  49. $limit -= strlen($data);
  50. $stop = $limit <= 0;
  51. }
  52. }
  53. }
  54. @fclose($fp);
  55. return $return;
  56. }
  57. }
复制代码

PHP


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!