方法1: 用file_get_contents 以get方式获取内容

<?php
$url='http://www.domain.com/';
$html = file_get_contents($url);
echo $html;
?> 方法2: 用fopen打开url, 以get方式获取内容 <?php
$fp = fopen($url, 'r');
//返回请求流信息(数组:请求状态,阻塞,返回值是否为空,返回值http头等)
stream_get_meta_data($fp);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
?> 方法3:用file_get_contents函数,以post方式获取url <?php
$data = array ('foo' => 'bar');
//生成url-encode后的请求字符串,将数组转换为字符串
$data = http_build_query($data);
$opts = array (
<span style="white-space:pre"> </span>'http' => array (
<span style="white-space:pre"> </span>'method' => 'POST',
<span style="white-space:pre"> </span>'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
<span style="white-space:pre"> </span>"Content-Length: " . strlen($data) . "\r\n",
<span style="white-space:pre"> </span>'content' => $data
<span style="white-space:pre"> </span>)
);
//生成请求的句柄文件
$context = stream_context_create($opts);
$html = file_get_contents('http://localhost/e/admin/test.html', false, $context);
echo $html;
?> 方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启 <?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()) {
$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;
}
?> 方法5:用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;
}
?> 方法6:使用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发送get、post请求获取内容的几种方法的更多相关文章

  1. 【springmvc Request】 springmvc请求接收参数的几种方法

    通过@PathVariabl注解获取路径中传递参数 转载请注明出处:springmvc请求接收参数的几种方法 代码下载地址:http://www.zuida@ima@com/share/1751862 ...

  2. spring 获取 WebApplicationContext的几种方法

    spring 获取 WebApplicationContext的几种方法 使用ContextLoader WebApplicationContext webApplicationContext = C ...

  3. PHP中获取星期的几种方法

    PHP中获取星期的几种方法   PHP星期几获取代码: 1 date(l); 2 //data就可以获取英文的星期比如Sunday 3 date(w); 4 //这个可以获取数字星期比如123,注意0 ...

  4. VC++获取IDC_EDIT的7种方法

    VC++获取IDC_EDIT的7种方法 http://blog.csdn.net/baizengfei/article/details/7997618 //第一种方法 int number1, num ...

  5. CSS中隐藏内容的3种方法及属性值

    CSS中隐藏内容的3种方法及属性值 (2011-02-11 13:33:59)   在制作网页时,隐藏内容也是一种比较常用的手法,它的作用一般有:隐藏文本/图片.隐藏链接.隐藏超出范围的内容.隐藏弹出 ...

  6. C#获取当前路径的7种方法

    总结C#获取当前路径的7种方法 C#获取当前路径的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName ...

  7. linux中快速清空文件内容的几种方法

    这篇文章主要介绍了linux中快速清空文件内容的几种方法,需要的朋友可以参考下 $ : > filename $ > filename $ echo "" > f ...

  8. Java获取随机数的几种方法

    Java获取随机数的几种方法 .使用org.apache.commons.lang.RandomStringUtils.randomAlphanumeric()取数字字母随机10位; //取得一个3位 ...

  9. 织梦首页、列表页调用文章body内容的两种方法

    http://blog.csdn.net/langyu1021/article/details/52261411 关于首页.列表页调用文章body内容的两种方法,具体方法如下: 第一种方法: {ded ...

随机推荐

  1. 注册表中LEGACY残留项的清理技巧

    http://bbs.kafan.cn/thread-889517-1-1.html 注册表中LEGACY残留项的清理技巧 2.Windows Vista系统 Windows XP系统下的修改权限的方 ...

  2. Java中SJBArrayList自己简单实现ArrayList

    /** * 自己实现ArrayList * @author zyyt * */ public class SJBArrayList { //存放SJBArrayList中的元素 transient O ...

  3. jQuery第四章

    jQuery中的事件和动画 一.jQuery中的事件 1.加载DOM (1)执行时机 $(document).ready()方法和window.onload方法有相似的功能,但是在执行时机方面是有区别 ...

  4. 更改自身web项目的图标(默认为tomcat的小喵咪)

    在页面<head>标签中加入 <link rel="shortcut icon" href="img/11.png" type="i ...

  5. 强大DOM选择器querySelector

    今天碰到问题,用了下不经常用的querySelector还不错 querySelector 和 querySelectorAll 的使用非常的简单,就像标题说到的一样,它和 CSS 的写法完全一样,对 ...

  6. hdu_5889_Barricade(最小割+最短路)

    题目链接:hdu_5889_Barricade 题意: 有n个点,m条边,每个边的长度都为1,每个边有一个消耗w,如果要阻断这条路,那么就会消耗w,现在让你阻断点1到点n的所有最短路,问你最小的消耗是 ...

  7. GROUP_CONCAT()多条数据.拼接字符串 最大长度1024

    mysql cmd show variables like "group_concat_max_len"; concat  合并多条数据 成一个逗号分隔的字符串 查找mysql的配 ...

  8. 4、Spring+MyBatis增删改查

    0.oracle数据库脚本 create table userinfo (id ), name ), password telephone ), isadmin )); --4.2 用户表序列 cre ...

  9. OGRE HelloWorld

    #include <OGRE/ExampleApplication.h> #include <OGRE/Ogre.h> class EnvMapApplication : pu ...

  10. box-shadow 阴影

    text-shadow是给文本添加阴影效果,box-shadow是给元素块添加周边阴影效果.随着html5和CSS3的普及,这一特殊效果使用越来越普遍. 基本语法是{box-shadow:[inset ...