PHP study notes: collecting website content through curl
Oct 15, 2016 am 10:31 AMRegarding curl, please go to Baidu and I will go directly to the case.
First enable your curl extension, turn on the curl extension in the php.ini file, that is, cancel the semicolon of extension=php_curl.dll.
eg: Use curl to collect website content and output it to txt document:
Goal: Grab the homepage of this blog and output it to the document
<span style="font-size: 18px;"><?<span style="color: #000000;">php </span><span style="color: #800080;">$ch</span> = curl_init("http://www.cnblogs.com/jianqingwang/"<span style="color: #000000;">); </span><span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>("example_jianqingwang.txt", "w"<span style="color: #000000;">); curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_FILE, <span style="color: #800080;">$fp</span><span style="color: #000000;">); curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_HEADER, 0<span style="color: #000000;">); curl_exec(</span><span style="color: #800080;">$ch</span><span style="color: #000000;">); curl_close(</span><span style="color: #800080;">$ch</span><span style="color: #000000;">); </span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">); </span>?></span>
Effect:
eg: Capture website content and output directly
Goal: grab http://www.cnblogs.com/jianqingwang/ and output it directly
// 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);//Change to 0 and there will be no output
curl_setopt($ch, CURLOPT_HEADER, 0);
// 3. Execute and obtain the content of the HTML document
$output = curl_exec($ch);
// 4. Release the curl handle
curl_close($ch);
?>
<span style="font-size: 18px;">?</span>
Effect:
Note: The interface here is a little different, because the css and image addresses are both relative paths, so the images and css are invalid.
eg:

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)