Code 1: get content using file_get_contents ??? & Lt ;? Php $ urlwww.baidu.com; $ htmlfile_get_contents ($ url );?? Print_r ($ http_response_header );?? Ec ($ html );? Six methods for php to call remote URLs
Example code 1: Use file_get_contents to get content in get mode
???
$ Url = 'http: // www.baidu.com /';
$ Html = file_get_contents ($ url );
?? // Print_r ($ http_response_header );
?? Ec ($ html );
?? Printhr ();
?? Printarr ($ http_response_header );
Printhr ();
?? ?>
?
??? Example code 2: Use fopen to open a url and get the content
??
?? $ Fp = fopen ($ url, 'r ');
? ?
?? Printarr (stream_get_meta_da
Ta ($ fp ));
Printhr ();
?? While (! Feof ($ fp )){
?????? $ Result. = fgets ($ fp, 1024 );
}
Echo "url body :??? $ Result ";
?? Printhr ();
?? Fclose ($ fp );
?? ?>
Sample Code 3: Use the file_get_contents function to obtain the url in post mode.
?? ?? $ Data = array ('foo' => 'bar ');
?? $ Data = http_build_query ($ data );
?? $ Opts = array (
'Http' => array (
?? 'Method' => 'post ',
??? 'Header' => "Content-type: application/x-www-form-urlencoded \ r \ n ".
???? "Content-Length:". strlen ($ data). "\ r \ n ",
?? 'Content' => $ data
?? ),
?? );
$ Context = stream_context_create ($ opts );
$ Html = file_get_contents ('http: // localhost/e/admin/test.html ', false, $ context );
Echo $ html;
?>
Example code 4: Use the fsockopen function to open a url and get complete data, including header and body
Function get_url ($ url, $ cookie = false ){
$ Url = parse_url ($ url );
$ Query = $ url [path]. "? ". $ Url [query];
Ec ("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 );
?????? While ([email protected] ($ fp )){
???????? $ Result. = @ fgets ($ fp, 1024 );
?????? }
?????? Fclose ($ fp );
?????? Return $ result;
}
}
// Obtain the html part of the url and remove the header
Function GetUrlHTML ($ url, $ cookie = false ){
??? $ Rowdata = get_url ($ url, $ cookie );
??? If ($ rowdata)
??? {
??????? $ Body = stristr ($ rowdata, "\ r \ n ");
??????? $ Body = substr ($ body, 4, strlen ($ body ));
??????? Return $ body;
??? }
? ?
??? Return false;
}
?>
Example code 5: Use the fsockopen function to open the url and obtain the complete data in POST mode, including the header and body
Function HTTP_Post ($ URL, $ data, $ cookie, $ referrer = ""){
// Parsing the given URL
$ URL_Info = parse_url ($ URL );
// Building referrer
If ($ referrer = "") // if not given use this script as referrer
$ Referrer = "111 ";
// Making string from $ data
Foreach ($ data as $ key => $ value)
$ Values [] = "$ key =". urlencode ($ value );
$ Data_string = implode ("&", $ values );
// Find out which port is needed-if not given use standard (= 80)
If (! Isset ($ URL_Info ["port"])
$ URL_Info ["port"] = 80;
// 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 );
While (! Feof ($ fp )){
?????? $ Result. = fgets ($ fp, 1024 );
}
Fclose ($ fp );
Return $ result;
}
Printhr ();
?>
Example code 6: Use the curl Library. before using the curl library, you may need to check php. ini to see if the curl extension has been enabled.
$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.baidu.com /');
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ File_contents = curl_exec ($ ch );
Curl_close ($ ch );
Echo $ file_contents;
?>
About curl Library:
Http://curl.haxx.se/
Curl is a file transfer tool using URL syntax. it supports FTP, FTPS, http htpps scp sftp tftp telnet dictfile and LDAP. Curl supports SSL certificates, http post, http put, and FTP uploads, kerberos, HTT-based Upload, proxy, cookie, user + password proof, file transfer recovery, http proxy channel and a large number of other useful techniques
Function printarr (array $ arr)
{
??? Echo"
Row field count: ". count ($ arr )."
";
??? Foreach ($ arr as $ key => $ value)
??? {
????? ?
??????????? Echo "$ key = $ value ???
";
??? }
}
?>