抓取网页数据php(这里有新鲜出炉的PHP面向对象编程,程序狗速度看过来! )

优采云 发布时间: 2021-09-20 08:08

  抓取网页数据php(这里有新鲜出炉的PHP面向对象编程,程序狗速度看过来!

)

  下面是新发布的PHP面向对象编程。看看程序狗的速度

  PHP开源脚本语言PHP(外名:超文本预处理器,中文名:“超文本预处理器”)是一种通用的开源脚本语言。语法吸收了C语言、Java和Perl的特点。它入门门槛低,易学,应用广泛。它主要适用于web开发领域。PHP的文件后缀是PHP

  本文文章主要介绍用PHP捕获网络数据的四种常用方法。本文介绍了如何使用文件获取内容函数、fopen函数和curl库这三种常用的网络数据获取方法,并给出了代码示例。有需要的朋友可以向他们推荐

  本节的名称为fsockopen、curl和file_uuget_uuucontents,具体讨论了这三种方式的网络数据输入和输出汇总。关于fsockopen,我们谈了很多。让我们转到其他话题。下面是一些捕获网络数据的常用方法

  1.使用文件获取内容获取内容:

  

$url = 'http://localhost/test2.php';

$html = file_get_contents($url);

echo $html;

  2.使用fopen打开URL并获取内容

  

$url = 'http://localhost/test2.php';

$fp = fopen($url, 'r');

stream_get_meta_data($fp);

$result = '';

while(!feof($fp))

{

$result .= fgets($fp, 1024);

}

echo "url body: $result";

fclose($fp);

  3.使用file_uu获取内容函数通过post获取URL

  

$data = array(

'foo'=>'bar',

'baz'=>'boom',

'site'=>'www.jb51.net',

'name'=>'nowa magic');

$data = http_build_query($data);

//$postdata = http_build_query($data);

$options = array(

'http' => array(

'method' => 'POST',

'header' => 'Content-type:application/x-www-form-urlencoded',

'content' => $data

//'timeout' => 60 * 60 // 超时时间(单位:s)

)

);

$url = "http://localhost/test2.php";

$context = stream_context_create($options);

$result = file_get_contents($url, false, $context);

echo $result;

  4、使用curl库。在使用curl库之前,您可能需要检查php.ini是否已打开curl扩展

  

$url = 'http://localhost/test2.php?site=jb51.net';

$ch = curl_init();

$timeout = 5;

curl_setopt ($ch, CURLOPT_URL, $url);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

curl_close($ch);

echo $file_contents;

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线