4 Common methods of crawling network data in PHP
The name of this section is Fsockopen,curl and file_get_contents, specifically to explore these three ways of network data input and output of some of the summary. About Fsockopen Front has been talked about a lot, below began to transfer to others. Here is a brief list of some common ways to crawl network data.
1. Use file_get_contents to get the content in the Get mode:
?
1 2 3 |
$url = ' http://localhost/test2.php '; $html = file_get_contents ($url); Echo $html; |
2. Open the URL with fopen and get the content in Get mode
?
1 2 3 4 5 6 7 8 9 10 |
$url = ' http://localhost/test2.php '; $fp = fopen ($url, ' R '); Stream_get_meta_data ($FP); $result = "; while (!feof ($FP)) { $result. = Fgets ($fp, 1024); } echo "URL body: $result"; Fclose ($FP); |
3. Use the File_get_contents function to get the URL by post
?
1 2 3 4 5 6 7 8 9 11 + + /+ //+ / + + + |
$data = Array ( ' foo ' = ' bar ', ' baz ' = ' boom ', ' site ' = ' www.jb51.net ' , ' name ' = ' Nowa Magic '); $data = Http_build_query ($data); //$postdata = Http_build_query ($data); $options = Array ( ' http ' = = Array ( ' method ' = ' POST ', ' header ' = ' = ' Content-type:applica Tion/x-www-form-urlencoded ', ' content ' = $data //' timeout ' + 60 * 60//timeout (in s) ) ) ; $url = "http://localhost/test2.php"; $context = stream_context_create ($options); $result = file_get_contents ($url, False, $context); Echo $result; |
4. Using the Curl Library, before using the Curl Library, you may need to check to see if the php.ini has already turned on the curl extension
?
1 2 3 4 5 6 7 8 9 |
$url = ' http://localhost/test2.php?site=jb51.net '; $ch = Curl_init (); $timeout = 5; curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); $file _contents = curl_exec ($ch); Curl_close ($ch); echo $file _contents; |
http://www.bkjia.com/PHPjc/1011121.html www.bkjia.com true http://www.bkjia.com/PHPjc/1011121.html techarticle PHP 4 commonly used crawl network data methods The name of this section is Fsockopen,curl and file_get_contents, specifically to explore these three ways of network data input and output of some of the summary. ...