PHP read local file common functions (fopen and file_get_contents)
One: string file_get_contents ( string $filename [, bool $use_include_path = False [, resource $context [, int $offset = 1 [, int $maxlen ] ]])
The file_get_contents () function is the preferred method for reading the contents of a file into a string. If operating system support also uses memory-mapping techniques to enhance performance.
Attention:
< Span class= "type" > < Span style= "color: #ff0000;" > One: timeout issue:
< Span class= "type" > workaround: < Span class= "type" > at timeout return After the error is like JS in the settimeout like a try, the error more than 3 or 5 times after the confirmation is unable to connect the server and completely give up.
1. Increase the time limit for timeouts
Modify the File_get_contents delay to use the timeout parameter of the resource $context. (Note: Set_time_limit only sets the time-out for your PHP program, not the time-out for the file_get_contents function to read the URL)
$opts = Array (' http ' =>array ( ' method ' = ' = ' GET ', ' timeout ' =>60, )); $context = Stream_ Context_create ($opts); $html =file_get_contents (' http://www.jb51.net ', false, $context); Fpassthru ($FP);
2: Multiple attempts
$cnt =0;while ($cnt < 3 && ([email protected]_get_contents (' http ... ')) ===false) { $cnt + +;}
Second: Read large files
Workaround: Use PHP's file read function file_get_contents () for segmented reads
Reads the contents of length MaxLen at the position specified by the parameter offset. If it fails, file_get_contents () returns FALSE.
$str = $content =file_get_contents ("2.sql", false,null,1024*1024,1024); Echo $str;
You can use the Fread () function if you only want to fragment read for a smaller file and finish reading this
$fp =fopen (' 2.sql ', ' R '), while (!feof ($fp)) {$str. =fread ($fp, FileSize ($filename)/10),///read out the file 10 1//for processing}echo $str;
Three: Resolve file_get_contents unable to request HTTPS connection method
PHP.ini The default configuration, read the HTTPS link with file_get_contents, will report the following error
Error: Warning:fopen () [function.fopen]: Unable to find the wrapper "https"-do you forget to enable it when you configure D PHP?
The solution has 3:
1.windows PHP, only need to php.ini in front of the Extension=php_openssl.dll, delete, restart the service can be.
2.linux PHP, you must install the OpenSSL module, installed in the future can be accessed.
3. If the server you can not modify the configuration, then use the Curl function to replace the file_get_contents function, of course, is not a simple replacement ah. There is also a corresponding parameter configuration to use the Curl function properly.
The Curl function is encapsulated as follows:
function Http_request ($url, $timeout =30, $header =array ()) {if (!function_exists (' Curl_init ')) {throw New Exception (' Server not install curl '); } $ch = Curl_init (); curl_setopt ($ch, Curlopt_returntransfer, true); curl_setopt ($ch, Curlopt_header, true); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_timeout, $timeout); if (!emptyempty ($header)) {curl_setopt ($ch, Curlopt_httpheader, $header); } $data = Curl_exec ($ch); List ($header, $data) = Explode ("\r\n\r\n", $data); $http _code = Curl_getinfo ($ch, Curlinfo_http_code); if ($http _code = = 301 | | $http _code = = 302) {$matches = array (); Preg_match ('/location: (. *?) \n/', $header, $matches); $url = Trim (Array_pop ($matches)); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_header, false); $data = curL_exec ($ch); } if ($data = = False) {curl_close ($ch); } @curl_close ($ch); return $data; }
Four: parsing PHP file_get_contents to get the remote page garbled problem
PHP file_get_contents get remote page content, if it is gzip encoded, the returned string is encoded garbled
1, solve the method, find a Ungzip function to convert the next
2, add a prefix to your URL, so call
$content = file_get_contents ("compress.zlib://". $url);
The above code works regardless of whether the page is gzip compressed or not!
3: Using Curl
function Curl_get ($url, $gzip =false) { $curl = Curl_init ($url); curl_setopt ($curl, Curlopt_returntransfer, 1); curl_setopt ($curl, Curlopt_connecttimeout, ten); if ($gzip) curl_setopt ($curl, curlopt_encoding, "gzip"); The key here $content = curl_exec ($curl); Curl_close ($curl); return $content;}
V: Use file_get_contents ("Php://input", "R") to get the original form content
<form action= "action.php" method= "POST" > <input type= "text" name= "UserName" id= "UserName"/> <br/> <input type= "text" name= "Userpass" id= "Userpass"/><br/> <input type= " Submit "value=" OK "/> </form><form action=" action.php "method=" POST "> <input type=" text "Name=" UserName " id=" UserName "/><br/> <input type=" text "name=" Userpass " id=" Userpass "/>< br/> <input type= "Submit" value= "OK"/> </form>
<?php
$raw _post_data = file_get_contents (' php://input ', ' R ');
echo "-------\$_post------------------<br/>";
echo Var_dump ($_post). "<br/>";
echo "-------php://input-------------<br/>";
Echo $raw _post_data. "<br/>";
Other: File_get_content implementation post
function Post ($url, $post = null) { $context = array (); if (Is_array ($post)) { ksort ($post); $context [' http '] = Array ( ' timeout ' =>60, ' method ' = ' POST ', ' content ' = Http_build_query ($ Post, ', ' & ') ; } Return file_get_contents ($url, False, Stream_context_create ($context));} $data = Array ( ' name ' = = ' Test ', ' email ' = ' [email protected] ', ' submit ' + ' submit ',); Echo Pos T (' http://www.jb51.net ', $data);
Second: Using fopen
Open the instance code of a local file directly <?php //If our local file is a text named Xmlas.txt $filedemo = "Xmlas.txt"; $fpdemo = fopen ($filedemo, "R"); if ($fpdemo) {while (!feof ($fpdemo)) { //1000 reads the number of characters $datademo = fread ($fpdemo, +); } Fclose ($fpdemo); } Echo $datademo;
Three:the difference between fopen,file_get_contents,curl functions in PHP:
1.fopen/file_get_contents DNS queries are re-queried each time the request is made, and the DNS information is not cached. However, Curl automatically caches the DNS information. Requests for Web pages or images under the same domain name require only one DNS query. This greatly reduces the number of DNS queries. So curl has a much better performance than fopen/file_get_contents.
2.fopen/file_get_contents is using Http_fopen_wrapper when requesting HTTP, and will not keeplive. and curl can. This makes curl more efficient when multiple links are requested more than once.
The 3.fopen/file_get_contents function is affected by the configuration of the Allow_url_open option in the php.ini file. If the configuration is turned off, the function is invalidated. Curl is not affected by this configuration.
4.curl can simulate a variety of requests, such as post data, form submission, etc., users can customize the request according to their own needs. Instead, fopen/file_get_contents can only get data using the Get method.
File_get_contents gets a remote file when the result is present in a string Fiels function stores the array form
Therefore, I prefer to use curl to access the remote URL. PHP has a Curl module extension, the function is very powerful.
5:file_get_contents setting timeout may not work
$config [' context '] = stream_context_create (Array (' http ' = = = Array (' method ' = ' = ' "GET", ' timeout ' = 5// This time-out is not stable, often does not work)) ;
At this time, look at the server connection pool, you will find a bunch of similar errors
File_get_contents (http://***): failed to open stream ...
6: Performance Test results:
Tests on curl and file_get_contents:
File_get_contents crawl google.com required number of seconds:
2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
Time used by Curl:
0.68719101
0.64675593
0.64326
0.81983113
0.63956594
Conclusion:
1: Recommended for network data crawl stability requirements of high friends use the above curl_file_get_contents function, not only stable speed, but also fake browser to deceive the target address OH
2:file_get_contents treatment is often small, it feels very good with it. If your file is handled by a 1k+ person. Then your server CPU is waiting for a lift. So it is recommended that you use the Curl library when you write PHP code later.
function Http_request ($url, $timeout =30, $header =array ()) {
if (!function_exists (' Curl_init ')) {
throw new Exception (' Server not install curl ');
}
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_returntransfer, true);
curl_setopt ($ch, Curlopt_header, true);
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_timeout, $timeout);
if (!emptyempty ($header)) {
curl_setopt ($ch, Curlopt_httpheader, $header);
}
$data = curl_exec ($ch);
List ($header, $data) = Explode ("\r\n\r\n", $data);
$http _code = Curl_getinfo ($ch, Curlinfo_http_code);
if ($http _code = = 301 | | $http _code = = 302) {
$matches = Array ();
Preg_match ('/location: (. *?) \n/', $header, $matches);
$url = Trim (Array_pop ($matches));
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_header, false);
$data = curl_exec ($ch);
}
if ($data = = False) {
Curl_close ($ch);
}
@curl_close ($ch);
return $data;
}
Share seven: Document processing