Php get page content-PHP source code

Source: Internet
Author: User
Php retrieves page content Php retrieves page content

Method 1: Use file_get_contents to get the content in get mode.
 Method 2: Use the file_get_contents function to obtain the url in post mode.
 '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); $ ctx = stream_context_create ($ opts); $ html = @ file_get_contents ($ url ,'', $ ctx); if you need to transmit cookie data, set 'header' => "Content-type: application/x-www-form-urlencoded \ r \ n ". "Content-Length :". strlen ($ data ). "\ r \ n", changed to 'header' => "Content-type: application/x-www-form-urlencoded \ r \ n ". "Content-Length :". strlen ($ data ). "\ r \ n ". "cookie: cookie1 = c1; cookie2 = c2 \ r \ n"; Method 3: use fopen to open the url and get the content
 ": Echo" url body: $ result "; fclose ($ fp);?> Method 4: use fopen to open the url and obtain the content in post mode.
 'Bar2', 'foo3' => 'bar3'); $ data = http_build_query ($ data ); $ opts = array ('http' => array ('method' => 'post', 'header' => "Content-type: application/x-www-form-urlencoded \ r \ nCookie: cook1 = c3; cook2 = c4 \ r \ n ". "Content-Length :". strlen ($ data ). "\ r \ n", 'content' => $ data); $ context = stream_context_create ($ opts); $ html = fopen ('http: // www.test.com/zzzzzz.php? Id = i3 & id2 = I4', 'RB', false, $ context); $ w = fread ($ html, 1024); echo $ w;?> Method 5: Use the fsockopen function to open the url and get the complete data, including the header and body.
 Method 6: Use the fsockopen function to open the url and obtain the complete data in POST mode, including the header and body.
 $ 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 ;}?> Method 7: Use the curl Library. before using the curl library, you may need to check whether php. ini has enabled the curl extension.
 

The above is the page content obtained by php. For more information, see PHP Chinese website (www.php1.cn )!

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.