打印google.com主页内容(HTML)的两种简单方法:
file_get_contents()<?php
$content = file_get_contents("http://www.google.com/");
echo '<pre>'.htmlspecialchars($content).'</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
如果此方法失败(由于未启用URL fopen包装器,请使用下面的第二种方法).
cURL:<?php
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$content = file_get_contents_curl("http://www.google.com/");
echo '<pre>'.htmlspecialchars($content).'</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2408 次 |
| 最近记录: |