What is collection?
is to use PHP programs to capture information from other websites into our own database and website.
PHP production and collection technology:
There are three methods from the bottom socket to the high-level file operation function. Implement collection.
1. Use socket technology to collect:(Recommended learning:PHP programming from entry to proficiency)
Socket collection is the lowest level. It just establishes a long connection, and then we have to construct the http protocol string ourselves to send the request.
For example, if you want to get the content of Youku page, use socket to write as follows:
The printed result is as follows, including the returned header information and the source code of the page:
2. Use curl_a set of functions
curl encapsulates the HTTP protocol into many functions. You can directly pass the corresponding parameters, which reduces the writing time. The difficulty of HTTP protocol strings.
Prerequisite: The curl extension must be enabled in php.ini.
function getHTTPS($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); curl_close($ch); return $result; } var_dump(getHTTPS($url));
The printed results are as follows, including only the source code of the page:
3. Use file_get_contents directly (the top level)
Prerequisite: Set the url address that allows opening a network in php.ini.
//使用file_get_contents() $data=file_get_contents("http://www.youku.com"); var_dump($data);
The above is the detailed content of What does php use for data collection?. For more information, please follow other related articles on the PHP Chinese website!