1, file_get_contents
Get the data in a get way
$url = ' blog.csdn.net/guugle2010 '; $html = file_get_contents ($url); Echo $html;
Get data by post
$data = Array ( ' name ' = ' guugle ', ' blog ' = ' blog.csdn.net/guugle2010 ' ); $data = Http_build_query ($data); $options = Array (' http ' = = Array (' method ' = ' + ' POST ', ' header ' = ' + ' content-type:application/ X-www-form-urlencode ', ' content ' = $data ) ); $url = "http://localhost/test.php"; $context = Stream_context_create ($options); $result = File_get_contents ($url, False, $context); echo $result;
2, Fopen Way
$url = ' http://blog.csdn.net/guugle2010 '; $handle = fopen ($url, r); $html = "; while (!feof ($handle)) { $html. = fgets ($handle);} echo $html; fclose ($handle);
3. Curl Library, need to open curl extension
$url = ' http://blog.csdn.net/guugle2010 '; $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;
4. Open the link with Fsocketopen
Get the full data (including header and body) in a Get way
$url = ' http://blog.csdn.net/guugle2010 '; function Get_url ($url, $cookie =false) {$url = Parse_url ($url); $query = $url [' path ']. "?". $url [' query ']; echo "Query:". $query; $fp = Fsockopen ($url [' Host '], $url [' Port ']? $url [' Port ']:80, $errno, $errstr, 30); if (! $fp) {return false; } else {$request = "GET $query http/1.1\r\n"; $request. = "Host: $url [host]\r\n"; $request. = "connection:close\r\n"; if ($cookie) $request. = "Cookie: $cookie \ n"; $request. = "\ r \ n"; Fwrite ($fp, $request); $result = "; while (!feof ($fp)) {$result. = @fgets ($fp, 1024); } fclose ($FP); return $result; }}//gets the HTML portion of the URL, removing the headerfunction geturlhtml ($url, $cookie =false) {$rowdata = Get_url ($url, $cookie); if ($rowdata) {$body = Stristr ($rowdata, "\r\n\r\n"); $body =substr ($body, 4,strlen ($body)); return $body; } return false;} echo Get_url ($url); Echo geturlhtml ($url);
Post for full data (including header and body)
$url = ' http://blog.csdn.net/guugle2010 '; function Http_post ($URL, $data, $cookie, $referer = "") {//parsing the given URL $ Url_info=parse_url ($URL);//Building referrer if ($referer = = "")//if not given with this script as referrer $refe Rer= "blog.csdn.net";Making string from $data foreach ($data as $key = = $value) $values []= "$key =". UrlEncode ($value); $data _string=implode ("&", $values);//Find out which port was needed-if not given use standard (=80) if (!isset ($URL _info["Port"])) $URL _info["Port"]=80; $request = ";//Building post-request: $request. =" POST ". $URL _info[" path "] ." Http/1.1\n "; $request. = "Host:". $URL _info["host"]. " \ n "; $request. = "Referer: $referer \ n"; $request. = "content-type:application/x-www-form-urlencoded\n"; $request. = "Content-length:". strlen ($data _string). " \ n "; $request. = "connection:close\n"; $request. = "Cookie: $cookie \ n"; $request. = "\ n"; $request. = $data _string. " \ n "; $fp = Fsockopen ($URL _info["host"], $URL _info["Port"]); Fputs ($fp, $request); $result = "; while (!feof ($fp)) {$result. = fgets ($fp, 1024); } fclose ($FP); return $result;} $data = Array (' site ' = = 'blog.csdn.net/guugle2010', ' Name ' = ' guugle '); $cookie = '; $referer = ' http://blog.csdn.net/'; Echo http_post ($url, $data, $cookie, $referer);
The above describes the PHP classic crawl network data methods, including PHP, Web content, I hope that the PHP tutorial interested in a friend helpful.