Home  >  Article  >  php教程  >  PHP study notes: collecting website content through curl

PHP study notes: collecting website content through curl

WBOY
WBOYOriginal
2016-10-15 10:31:501322browse

Regarding 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:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn