1,file_get_contents

  1. <?php
  2. $content = file_get_contents($getPath);
  3. file_put_contents($savePath,$content);
  1. <?php
  2. $content = file_get_contents($getPath);
  3. //file_get_contents超时处理
  4. $opts = array(
  5. 'http'=>array(
  6. 'method'=>"GET",
  7. 'timeout'=>1,//单位秒
  8. )
  9. );
  10. $cnt=0;
  11. while($cnt<3 && ($content=file_get_contents($getPath, false, stream_context_create($opts)))===FALSE) $cnt++;
  12. file_put_contents($savePath,$content);
  1. <?php
  2. //乱码问题,特别是txt文件
  3. $file_content = mb_convert_encoding( file_get_contents($getPath, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );

2,fopen

  1. <?php
  2. $handle = fopen($getPath, 'r');
  3. while(false != ($a = fread($handle, 10240))){//返回false表示已经读取到文件末尾
  4. $content .= $a;
  5. }
  6. fclose($handle);
  1. <?php
  2. //分块读取并分块写入
  3. $handle = fopen($getPath, 'r'); //只读方式打开
  4. $toHandle = fopen($savePath,'a'); //写入方式打开,不存在则创建
  5. while(false != ($a = fread($handle, 10240))){//返回false表示已经读取到文件末尾
  6. fwrite($toHandle,$a);
  7. }
  8. fclose($toHandle);
  9. fclose($handle);
  1. <?php
  2. $handle = fopen($getPath, "rb");
  3. $getData = fread($handle, 8096);
  4. $toHandle = fopen($savePath, "wb");
  5. while ($getData) {
  6. fwrite($file, $getData);
  7. $getData = fread($handle, 8096);
  8. }
  9. fclose($handle);
  10. fclose($toHandle);
  1. <?php
  2. //$fileSize 文件大小
  3. $fp = fopen ( $file_path, "rb" ); // 打开文件
  4. $dstStream = fopen ( 'php://output', 'wb' ); //直接输出到浏览器
  5. $offset = 0;
  6. $chunksize = 4096; //分块大小
  7. while ( ! feof ( $fp ) && $offset < $fileSize ) {
  8. $last_size = $fileSize - $offset;
  9. if ($chunksize > $last_size && $last_size > 0) {
  10. $chunksize = $last_size;
  11. }
  12. $offset += stream_copy_to_stream ( $fp, $dstStream, $chunksize, $offset );
  13. }
  14. fclose ( $dstStream );
  15. fclose ( $fp );

3,fsockopen

参考来源:https://blog.csdn.net/navioo/article/details/82771663

  1. <?php
  2. /**
  3. * 使用fsockopen发送URL请求
  4. * @param $url
  5. * @param $method: GET、POST等
  6. * @param array $params
  7. * @param array $header
  8. * @param int $timeout
  9. * @return array|bool
  10. */
  11. function sendHttpRequest($url, $method = 'GET', $params = [], $header = [], $timeout = 30)
  12. {
  13. $urlInfo = parse_url($url);
  14. if (isset($urlInfo['scheme']) && strcasecmp($urlInfo['scheme'], 'https') === 0) //HTTPS
  15. {
  16. $prefix = 'ssl://';
  17. $port = 443;
  18. }else{ //HTTP
  19. $prefix = 'tcp://';
  20. $port = isset($urlInfo['port']) ? $urlInfo['port'] : 80;
  21. }
  22. $host = $urlInfo['host'];
  23. $path = isset($urlInfo['path']) ? $urlInfo['path'] : '/';
  24. if(!empty($params) && is_array($params))
  25. {
  26. $params = http_build_query($params);
  27. }
  28. $contentType = '';
  29. $contentLength = '';
  30. $requestBody = '';
  31. if($method === 'GET')
  32. {
  33. $params = $params ? '?' . $params : '';
  34. $path .= $params;
  35. }else{
  36. $requestBody = $params;
  37. $contentType = "Content-Type: application/x-www-form-urlencoded\r\n";
  38. $contentLength = "Content-Length: " . strlen($requestBody) . "\r\n";
  39. }
  40. $auth = '';
  41. if(!empty($urlInfo['user']))
  42. {
  43. $auth = 'Authorization: Basic ' . base64_encode($urlInfo['user'] . ':' . $urlInfo['pass']) . "\r\n";
  44. }
  45. if($header && is_array($header))
  46. {
  47. $tmpString = '';
  48. foreach ($header as $key => $value)
  49. {
  50. $tmpString .= $key . ': ' . $value . "\r\n";
  51. }
  52. $header = $tmpString;
  53. }else{
  54. $header = '';
  55. }
  56. $out = "$method $path HTTP/1.1\r\n";
  57. $out .= "Host: $host\r\n";
  58. $out .= $auth;
  59. $out .= $header;
  60. $out .= $contentType;
  61. $out .= $contentLength;
  62. $out .= "Connection: Close\r\n\r\n";
  63. $out .= $requestBody;//post发送数据前必须要有两个换行符\r\n
  64. $fp = fsockopen($prefix . $host, $port, $errno, $errstr, $timeout);
  65. if(!$fp)
  66. {
  67. return false;
  68. }
  69. if(!fwrite($fp, $out))
  70. {
  71. return false;
  72. }
  73. $response = '';
  74. while(!feof($fp))
  75. {
  76. $response .= fread($fp, 1024);
  77. }
  78. if(!$response)
  79. {
  80. return false;
  81. }
  82. fclose($fp);
  83. $separator = '/\r\n\r\n|\n\n|\r\r/';
  84. list($responseHeader, $responseBody) = preg_split($separator, $response, 2);
  85. $httpResponse = array(
  86. 'header' => $responseHeader,
  87. 'body' => $responseBody
  88. );
  89. return $httpResponse;
  90. }