PHP learning notes: Using curl to collect website content and learning notes curl
For curl, please join Baidu and I will discuss the case directly.
First, enable your curl extension. In the php. ini file, enable the curl extension, that is, cancel the semicolon of extension = php_curl.dll.
Eg: Use curl to collect website content and output it to the txt document:
Objective: To capture the homepage of this blog and output it to the document
<?php$ch = curl_init("http://www.cnblogs.com/jianqingwang/");$fp = fopen("example_jianqingwang.txt", "w");curl_setopt($ch, CURLOPT_FILE, $fp);curl_setopt($ch, CURLOPT_HEADER, 0);curl_exec($ch);curl_close($ch);fclose($fp);?>
Effect:
Eg: crawls website content and outputs it directly
Target: capture http://www.cnblogs.com/jianqingwang/and directly output
<?
// 1. Initialization
$ Ch = curl_init ();
// 2. Set options, including URL
Curl_setopt ($ ch, CURLOPT_URL, "http://www.cnblogs.com/jianqingwang ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0); // if it is set to 0, no output will be made.
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
// 3. Execute and obtain the HTML document content
$ Output = curl_exec ($ ch );
// 4. Release the curl handle
Curl_close ($ ch );
?>
Effect:
Note: The interface here is a bit different because css and image addresses are both relative paths, so images and css are invalid.
Eg: