方法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抓取网页内容的几种方法的更多相关文章

  1. php抓取页面的几种方法详解

    本篇文章是对php抓取页面的几种方法进行了详细的分析介绍,需要的朋友参考下 在 做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求 ...

  2. php抓取页面的几种方式

    在做一些天气预报或者RSS订阅的程序时,往往 需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求访问url地址, 然后得到html源代码或者xml数据,得到数据我们不能直接 ...

  3. C# 抓取网页内容的方法

    1.抓取一般内容 需要三个类:WebRequest.WebResponse.StreamReader 所需命名空间:System.Net.System.IO 核心代码: view plaincopy ...

  4. ASP.NET抓取网页内容的实现方法

    这篇文章主要介绍了ASP.NET抓取网页内容的实现方法,涉及使用HttpWebRequest及WebResponse抓取网页内容的技巧,需要的朋友可以参考下 一.ASP.NET 使用HttpWebRe ...

  5. PHP几种抓取网络数据的常见方法

    //本小节的名称为 fsockopen,curl与file_get_contents,具体是探讨这三种方式进行网络数据输入输出的一些汇总.关于 fsockopen 前面已经谈了不少,下面开始转入其它. ...

  6. paip.抓取网页内容--java php python

    paip.抓取网页内容--java php python.txt 作者Attilax  艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...

  7. Asp.Net 之 抓取网页内容

    一.获取网页内容——html ASP.NET 中抓取网页内容是非常方便的,而其中更是解决了 ASP 中困扰我们的编码问题. 需要三个类:WebRequest.WebResponse.StreamRea ...

  8. c#抓取网页内容乱码的解决方案

    写过爬虫的同学都知道,这是个很常见的问题了,一般处理思路是: 使用HttpWebRequest发送请求,HttpWebResponse来接收,判断HttpWebResponse中”Content-Ty ...

  9. PHP获取网页内容的几种方法

    方法1: 用file_get_contents以get方式获取内容 <?php $url='http://www.domain.com/?para=123'; $html= file_get_c ...

随机推荐

  1. 在EA中将画出的ER图转换成SQL脚本

    在进行数据库设计的时候,想着正好安装着EA软件呢,看能不能用EA画ER模型.结果发现不仅能画.并且还能进行整套数据库设计(生成SQL脚本). 以下以机房收费系统用户--学生为例.学生能够查看剩余金额. ...

  2. 面试题:Student s = new Student();在内存中做了哪些事情?即创建一个对象做了哪些事情

    lStudent s = new Student();在内存中做了哪些事情? •载入Student.class文件进内存(方法区) •在栈内存为s开辟空间 •在堆内存为学生对象开辟空间 •对学生对象的 ...

  3. vim 插件之supertab

    supertab.vim 这个插件是用来把tab键作为只能补全的映射,当然,具体更能肯定不仅仅只是如此,等待以后发现吧 地址 http://github.com/ervandew/supertab h ...

  4. 查看suse系统版本

    cat /etc/*-release OR lsb_release -d

  5. SQL Server单表已700w+将普通表转换成分区表

    最近项目中,某个表数据量爆发时增长,单表已700w+,读写性能急剧下降,所以考虑加入分区表以解燃眉之急,后续还是要分表分库,当然这是后话.下面简要说一下将普通表转为分区表的步骤.   一.创建文件组 ...

  6. BZOJ 2049 LCT

    思路:LCT的基本操作 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm&g ...

  7. Idea的一些调试技巧及设置todo

    程序员的工作内容,除了大部分时间写代码之外,因为有不少的时间是用在调试代码上.甚至说不是在调试代码,就是即将调试代码. :) 今天我们来谈谈调试代码的一些技巧,在使用IDE提供的debugger时一些 ...

  8. Spring控制反转容器的使用例子

    详细代码如下: spring-config.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  9. ubuntu重启网络报错

    执行:gw@ubuntu:/$ /etc/init.d/networking restart 报错:stop: Rejected send message, 1 matched rules; type ...

  10. NodeJS学习笔记 进阶 (13)Nodejs进阶:5分钟入门非对称加密用法

    个人总结:读完这篇文章需要5分钟,这篇文章讲解了Node.js非对称加密算法的实现. 摘录自网络 地址: https://github.com/chyingp/nodejs-learning-guid ...