PHP抓取网页内容的几种方法
方法1: 用file_get_contents 以get方式获取内容
<?php
$url='http://www.domain.com/?para=123';
$html = file_get_contents($url);
echo $html;
?> 方法2:用file_get_contents函数,以post方式获取url
<?php
$url = 'http://www.domain.com/test.php?id=123';
$data = array ('foo' => 'bar');
$data = http_build_query($data); $opts = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$ctx = stream_context_create($opts);
$html = @file_get_contents($url,'',$ctx); 如果需要再传递cookie数据,则把
'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
修改为
'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($data) . "\r\n".
"cookie:cookie1=c1;cookie2=c2\r\n" ;
即可 方法3: 用fopen打开url, 以get方式获取内容
<?php
$fp = fopen($url, 'r');
$header = stream_get_meta_data($fp);//获取报头信息
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo "url header: {$header} <br>":
echo "url body: $result";
fclose($fp);
?> 方法4: 用fopen打开url, 以post方式获取内容
<?php
$data = array ('foo2' => 'bar2','foo3'=>'bar3');
$data = http_build_query($data); $opts = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
); $context = stream_context_create($opts);
$html = fopen('http://www.test.com/zzzz.php?id=i3&id2=i4','rb' ,false, $context);
$w=fread($html,1024);
echo $w;
?> 方法5:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body
<?php
function get_url ($url,$cookie=false)
{
$url = parse_url($url);
$query = $url[path]."?".$url[query];
echo "Query:".$query;
$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$request = "GET $query HTTP/1.1\r\n";
$request .= "Host: $url[host]\r\n";
$request .= "Connection: Close\r\n";
if($cookie) $request.="Cookie: $cookie\n";
$request.="\r\n";
fwrite($fp,$request);
while(!@feof($fp)) {
$result .= @fgets($fp, 1024);
}
fclose($fp);
return $result;
}
}
//获取url的html部分,去掉header
function GetUrlHTML($url,$cookie=false)
{
$rowdata = get_url($url,$cookie);
if($rowdata)
{
$body= stristr($rowdata,"\r\n\r\n");
$body=substr($body,4,strlen($body));
return $body;
} return false;
}
?> 方法6:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body
<?php
function HTTP_Post($URL,$data,$cookie, $referrer="")
{ // parsing the given URL
$URL_Info=parse_url($URL); // Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer="111"; // making string from $data
foreach($data as $key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values); // Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80; // building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
$request.="Host: ".$URL_Info["host"]."\n";
$request.="Referer: $referer\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($data_string)."\n";
$request.="Connection: close\n"; $request.="Cookie: $cookie\n"; $request.="\n";
$request.=$data_string."\n"; $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
fclose($fp); return $result;
} ?> 方法7:使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
<?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.domain.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch); echo $file_contents;
?>
PHP抓取网页内容的几种方法的更多相关文章
- php抓取页面的几种方法详解
本篇文章是对php抓取页面的几种方法进行了详细的分析介绍,需要的朋友参考下 在 做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求 ...
- php抓取页面的几种方式
在做一些天气预报或者RSS订阅的程序时,往往 需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求访问url地址, 然后得到html源代码或者xml数据,得到数据我们不能直接 ...
- C# 抓取网页内容的方法
1.抓取一般内容 需要三个类:WebRequest.WebResponse.StreamReader 所需命名空间:System.Net.System.IO 核心代码: view plaincopy ...
- ASP.NET抓取网页内容的实现方法
这篇文章主要介绍了ASP.NET抓取网页内容的实现方法,涉及使用HttpWebRequest及WebResponse抓取网页内容的技巧,需要的朋友可以参考下 一.ASP.NET 使用HttpWebRe ...
- PHP几种抓取网络数据的常见方法
//本小节的名称为 fsockopen,curl与file_get_contents,具体是探讨这三种方式进行网络数据输入输出的一些汇总.关于 fsockopen 前面已经谈了不少,下面开始转入其它. ...
- paip.抓取网页内容--java php python
paip.抓取网页内容--java php python.txt 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...
- Asp.Net 之 抓取网页内容
一.获取网页内容——html ASP.NET 中抓取网页内容是非常方便的,而其中更是解决了 ASP 中困扰我们的编码问题. 需要三个类:WebRequest.WebResponse.StreamRea ...
- c#抓取网页内容乱码的解决方案
写过爬虫的同学都知道,这是个很常见的问题了,一般处理思路是: 使用HttpWebRequest发送请求,HttpWebResponse来接收,判断HttpWebResponse中”Content-Ty ...
- PHP获取网页内容的几种方法
方法1: 用file_get_contents以get方式获取内容 <?php $url='http://www.domain.com/?para=123'; $html= file_get_c ...
随机推荐
- jsp urlrewrite 中正則表達式不包括某个字符串写法
因在程序中须要做城市间跳转,可是页面中包括的css.scripts和图片等路径是要排除在外的. 这就须要在正则中指定当遇到哪些 字符时须要略过. 正则例如以下: /((? !css)(?!script ...
- hdu 4771 求一点遍历全部给定点的最短路(bfs+dfs)
题目如题.题解如题. 因为目标点最多仅仅有4个,先bfs出俩俩最短路(包含起点).再dfs最短路.)0s1A;(当年弱跪杭州之题,现看如此简单) #include<iostream> #i ...
- 基于深度学习的病毒检测技术无需沙箱环境,直接将样本文件转换为二维图片,进而应用改造后的卷积神经网络 Inception V4 进行训练和检测
话题 3: 基于深度学习的二进制恶意样本检测 分享主题:全球正在经历一场由科技驱动的数字化转型,传统技术已经不能适应病毒数量飞速增长的发展态势.而基于沙箱的检测方案无法满足 APT 攻击的检测需求,也 ...
- PowerDesigner怎么调出工具箱?
PowerDesigner怎么调出工具箱? 如图操作:
- 基于Struts2+MySQL的多表出差明细表单
下载地址:http://download.csdn.net/detail/qq_33599520/9790629 项目结构: UserAction package com.mstf.action; i ...
- iOS开发下对MVVM的理解
最近看到新浪微博上以及iOS开发的论坛里面谈到MVVM设计模式,所谓MVVM就是Model-View-ViewModel的缩写,关于MVVM的概念,这里我不想过多的介绍,有很多介绍的很详细的博文,这里 ...
- PostgreSQL Replication之第三章 理解即时恢复(1)
到现在为止,您已经掌握了一定的理论.因为生活不仅由理论组成(它可能同样重要),是时候深入实际的工作了. 本章的目标是让您明白如何恢复数据到一个给定的时间点.当您的系统崩溃或者有人意外地删除了一个表,不 ...
- UVa 140 Bandwidth【枚举排列】
题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...
- ubuntu重启网络报错
执行:gw@ubuntu:/$ /etc/init.d/networking restart 报错:stop: Rejected send message, 1 matched rules; type ...
- Display Video
###chromium webrtc视频显示 ###两个数据源:本地数据源: MediaStreamRemoteVideoSource(content/renderer/media/webrtc/me ...