FOP PHP Read page file content implementation code FOPEN,CURL, etc.

Source: Internet
Author: User
1.fopen Implementation code:

Copy the Code code as follows:


$handle = fopen ("http://www.example.com/", "RB");
$contents = "";
while (!feof ($handle)) {
$contents. = Fread ($handle, 8192);
}
Fclose ($handle);
?>


Copy the Code code as follows:


For PHP 5 and later
$handle = fopen ("http://www.example.com/", "RB");
$contents = Stream_get_contents ($handle);
Fclose ($handle);
?>


2.curl Implementation code:

Copy the Code code as follows:


function _url ($Date) {
$ch = Curl_init ();
$timeout = 5;
curl_setopt ($ch, Curlopt_url, "$Date");
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
$contents = curl_exec ($ch);
Curl_close ($ch);
return $contents;
}
$pageURL = "http://www.baidu.com";
$c
?>


Encoding Conversion functions

Copy the Code code as follows:


$html = file_get_contents ("http://s.jb51.net");
$html = Iconv ("Big5", "Utf-8//ignore", $html); Conversion encoding mode is UTF8
Print $html;
$htm = File ("Http://s.jb51.net");
$h = "";
foreach ($htm as $value)
{
$h. = Iconv ("GB2312", "Utf-8//ignore", $value);
}
Print_r ($h);


Another way to open a Web page

Copy the Code code as follows:


$opts = Array (
' HTTP ' =>array (
' Method ' = ' GET ',
' Header ' = ' accept-language:en\r\n '.
"Cookie:foo=bar\r\n"
)
);
$context = Stream_context_create ($opts);
/* Sends an HTTP request to www.example.com
With additional headers shown above */
$fp = fopen (' http://www.baidu.com ', ' R ', false, $context);
Fpassthru ($FP);
Fclose ($FP);
?>

The above describes the FOP PHP read page file content implementation code Fopen,curl, including the FOP aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.