Sometimes we need to do some collection to download the remote Web page source code to the original, here we have sorted out some php to get the remote Web page source code, I hope you will be helpful. What are the basic examples of php curl functions? Php initializes... sometimes we need to do some collection to download the remote Web page source code to the original, here we have sorted out some php to get the remote Web page source code, I hope you will be helpful.
Php curl function
Basic example
Php fopen function
n");}// close the filefclose($myFile);print ("FTPn");print ("n");// open a file using ftp protocolif (!($myFile = fopen("ftp://ftp.php.net/welcome.msg", "r"))) { print ("file could not be opened"); exit;}while (!feof($myFile)) { // read a line from the file $myLine = fgetss($myFile, 255); print ("$myLine
n");}// close the filefclose($myFile);print ("Localn");print ("n");// open a local fileif (!($myFile = fopen("data.txt", "r"))) { print ("file could not be opened"); exit;}while (!feof($myFile)) { // read a line from the file $myLine = fgetss($myFile, 255); print ("$myLine
n");}// close the filefclose($myFile);?>
File_get_contents function
Capture remote webpage source code
_ Url; $ pos = strpos ($ req, ': //'); $ this-> _ protocol = strtolower (substr ($ req, 0, $ pos )); $ req = substr ($ req, $ pos + 3); $ pos = strpos ($ req, '/'); if ($ pos = false) $ pos = strlen ($ req); $ host = substr ($ req, 0, $ pos); if (strpos ($ host ,':')! = False) {list ($ this-> _ host, $ this-> _ port) = explode (':', $ host );} else {$ this-> _ host = $ host; $ this-> _ port = ($ this-> _ protocol = 'https ')? 443: 80 ;}$ this-> _ uri = substr ($ req, $ pos); if ($ this-> _ uri = '') $ this-> _ uri = '/';} // constructor function HTTPRequest ($ url) {$ this-> _ url = $ url; $ this-> _ scan_url ();} // download URL to string function DownloadToString () {$ crlf = "rn"; // generate request $ req = 'get '. $ this-> _ uri. 'HTTP/1.0 '. $ crlf. 'host :'. $ this-> _ host. $ crlf. $ crlf; // fetch $ this-> _ fp = fsockopen ($ this-> _ Protocol = 'https '? 'SSL ://':''). $ this-> _ host, $ this-> _ port); fwrite ($ this-> _ fp, $ req); while (is_resource ($ this-> _ fp) & $ this-> _ fp &&! Feof ($ this-> _ fp) $ response. = fread ($ this-> _ fp, 1024); fclose ($ this-> _ fp); // split header and body $ pos = strpos ($ response, $ crlf. $ crlf); if ($ pos = false) return ($ response); $ header = substr ($ response, 0, $ pos ); $ body = substr ($ response, $ pos + 2 * strlen ($ crlf); // parse headers $ headers = array (); $ lines = explode ($ crlf, $ header); foreach ($ lines as $ line) if ($ pos = strpos ($ line ,':'))! = False) $ headers [strtolower (trim (substr ($ line, 0, $ pos)] = trim (substr ($ line, $ pos + 1 )); // redirection? If (isset ($ headers ['location']) {$ http = new HTTPRequest ($ headers ['location']); return ($ http-> DownloadToString ($ http);} else {return ($ body) ;}}// usage $ r = new HTTPRequest (' http://www.phprm.com '); $ Str = $ r-> DownloadToString ();?>