Home  >  Article  >  Backend Development  >  PHP uses CURL to download remote HTML files

PHP uses CURL to download remote HTML files

WBOY
WBOYOriginal
2016-07-25 09:08:341192browse
It is said that using Curl is more efficient than file_get_contents when downloading remote HTML files.                                                                                                                       
                                                                                                                                                                                                                                                                                                       



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//Set the URL, which can be put into curl_init parameters
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1");
  1. //Set UA
  2. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  3. //Convert the information obtained by curl_exec() Return as a file stream rather than output directly. If not added, even if there is no echo, it will automatically output
  4. $content = curl_exec($ch);
  5. //Execute
  6. curl_close($ch);
  7. //Close
  8. echo $content;
  9. Copy code
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