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([email protected]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;

?>

注明:本文转载自https://www.oschina.net/code/snippet_861770_19638;

时间: 2024-08-02 11:28:07

php获取网页内容的方法--(转载)的相关文章

asp.net获取客户端IP方法(转载)

最近web获取客户端ip,看到下面这篇文章,转载过来,一起分享(转载地址:http://www.cnblogs.com/yejun/archive/2008/02/26/1082485.html) 通常我们都通过下面的代码获得IP: string ip =System.Web.HttpContext.Current.Request.UserHostAddress; 或 string ip =System.Web.HttpContext.Current.Request.ServerVariable

定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容。提示(可以了解python的urllib模块)

1 定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容.提示(可以了解python的urllib模块) 2 import urllib.request 3 4 def get_page(url): 5 response = urllib.request.urlopen(url) 6 html = response.read() 7 return html 8 9 print(get_page(url='https://www.baidu,com'))

使用cvs或svn从sourceforge上获取开源项目的方法[转载]

著名开源软件网站(www.sourceforge.net)上面的开源项目,大部分使用的管理工具为cvs或svn. 这两种软件的代表客户端程序是wincvs和tortoiseSVN. 1.cvs CVS (Concurrent Versions System) is a tool used by many software developers to manage changes within their source code tree. CVS provides the means to st

总结C#获取当前路径的7种方法(转载)

总结C#获取当前路径的7种方法 C#获取当前路径的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -获取模块的完整路径. 2. System.Environment.CurrentDirectory -获取和设置当前目录(该进程从中启动的目录)的完全限定目录. 3. System.IO.Directory.GetCurrentDirectory() -获取应用程序的当前工作目录.这个不一定是程

【转载】获取MAC地址方法大全

From:http://blog.csdn.net/han2814675/article/details/6223617 Windows平台下用C++代码取得机器的MAC地址并不是一件简单直接的事情.到目前为止,作者尚未发现有任何一个通用的100%的适用于所有Windows平台的方法可以稳定的取得MAC地址.而有些应用(比如MMORPG)则需要稳定的得到机器的MAC地址,解决方案往往是通过多种方法依次使用来提高成功率. 说明: 以下方法只会返回多网卡的第一个MAC地址. 网上有很多文章和源码来解

[PHP学习教程]006.获取网页内容(URL Content)

引言:获取网页内容是我们实现网页操作的基本之基本,今天这一讲,我们和大家讲一下基本请求网页内容的几种方法. 我们似乎每天都要做这样一件事情,打开一个浏览器,输入网址,回车,一个空白的页面顿时有了东西,它可能是百度之类的搜索页面,或是一个挤满了文字和图片的门户网站. 我们可以从三个方面理解这个过程,一个是浏览器,二个是服务器,第三个是浏览器和服务器之间通信的协议. 当然,我们今天不讲<网页请求过程 > 这一次,我们说一下如何用PHP代码请求网页内容. 获取网页内容方法 1.file_get_co

【C#】获取网页内容及HTML解析器HtmlAgilityPack的使用

最近经常需要下载一些东西,而这个下载地址又会经过层层跳转,每个页面上都有很多广告,烦不胜烦,所以做了一个一键获得最终下载地址的小工具.使用C#,来获取网页内容,然后通过HtmlAgilityPack获取某a标签的href,不断循环,层层跳转,最后获得最终下载地址. 下面,介绍HtmlAgilityPack的使用方法,这个方法也是从网上很多篇文章中拼凑摸索出来的,因为找了一大圈根本找不到HtmlAgilityPack的文档…… 首先,using HtmlAgilityPack; 代码片段: str

Arcengine 实现要素选取的方法(转载)

转自原文Arcengine 实现要素选取的方法(转载) 选择一个要素或者一个要素集(FeatureSelection)的方法很多,如IMap::SelectByShape.ILayer::search.IFeatureSection::SelectFeature等方法 主要用到的方法: IMap接口的SelectFeature(Layer, Feature) (方法,从一个Layer中选择一个Feature): IMap接口SelectByShape(Shape, env, justOne) (

java获得采集网页内容的方法小结

      为了写一个java的采集程序,从网上学习到3种方法可以获取单个网页内容的方法,主要是运用到是java IO流方面的知识,对其不熟悉,因此写个小结. import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; impo