PHP抓取网页的方法有多种,以下是其中一种常用的方法:
$url = "http://example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
$url = "http://example.com";
$response = file_get_contents($url);
echo $response;
$url = "http://example.com";
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
),
));
$handle = fopen($url, 'r', false, $context);
$response = stream_get_contents($handle);
fclose($handle);
echo $response;
以上是几种常用的PHP抓取网页的方法,根据实际需求选择合适的方法来获取网页数据。
上一篇:双引号在php
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站