Php program code for obtaining remote webpage source code

Source: Internet
Author: User
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 ();?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.