php发送get、post请求获取内容的几种方法
方法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请求获取内容的几种方法的更多相关文章
- 【springmvc Request】 springmvc请求接收参数的几种方法
通过@PathVariabl注解获取路径中传递参数 转载请注明出处:springmvc请求接收参数的几种方法 代码下载地址:http://www.zuida@ima@com/share/1751862 ...
- spring 获取 WebApplicationContext的几种方法
spring 获取 WebApplicationContext的几种方法 使用ContextLoader WebApplicationContext webApplicationContext = C ...
- PHP中获取星期的几种方法
PHP中获取星期的几种方法 PHP星期几获取代码: 1 date(l); 2 //data就可以获取英文的星期比如Sunday 3 date(w); 4 //这个可以获取数字星期比如123,注意0 ...
- VC++获取IDC_EDIT的7种方法
VC++获取IDC_EDIT的7种方法 http://blog.csdn.net/baizengfei/article/details/7997618 //第一种方法 int number1, num ...
- CSS中隐藏内容的3种方法及属性值
CSS中隐藏内容的3种方法及属性值 (2011-02-11 13:33:59) 在制作网页时,隐藏内容也是一种比较常用的手法,它的作用一般有:隐藏文本/图片.隐藏链接.隐藏超出范围的内容.隐藏弹出 ...
- C#获取当前路径的7种方法
总结C#获取当前路径的7种方法 C#获取当前路径的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName ...
- linux中快速清空文件内容的几种方法
这篇文章主要介绍了linux中快速清空文件内容的几种方法,需要的朋友可以参考下 $ : > filename $ > filename $ echo "" > f ...
- Java获取随机数的几种方法
Java获取随机数的几种方法 .使用org.apache.commons.lang.RandomStringUtils.randomAlphanumeric()取数字字母随机10位; //取得一个3位 ...
- 织梦首页、列表页调用文章body内容的两种方法
http://blog.csdn.net/langyu1021/article/details/52261411 关于首页.列表页调用文章body内容的两种方法,具体方法如下: 第一种方法: {ded ...
随机推荐
- UVa 10305 Ordering Tasks (例题 6-15)
传送门: https://uva.onlinejudge.org/external/103/10305.pdf 拓扑排序(topological sort)简单题 自己代码的思路来自: ==> ...
- 2016 SyScan360 国际前瞻信息安全会议 多角度探讨信息安全
SyScan360国际前瞻信息安全会议由与中国第一大互联网安全公司-360公司与SyScan前瞻信息安全技术年会(TheSymposiumonSecurityforAsiaNetwork,以下简称Sy ...
- <?php function say() { echo 'hello world'; } //在这里调用函数 say(); php 调用方法say()
<?php function say() { echo 'hello world'; } //在这里调用函数 say(); php 调用方法say()
- 有关Flash中与Java调用时候注意的一些事项
1.构建本地拓展的ActionScript库时候要注意SWF文件的版本兼容性: 参考:http://help.adobe.com/zh_CN/air/extensions/WS99209310cacd ...
- php中 xml json 数组 之间相互转换
php中 xml json 数组 之间相互转换 1 数组转json $result = array( 'status' =>$status, 'message'=>$message, ' ...
- HDU 1969 Pie(二分搜索)
题目链接 Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pi ...
- redhat 时区修改
修改系统时区,发现只需要一个命令就可以解决 修改之前: cp /usr/share/zoneinfo/Asia/Chongqing /etc/localtime 修改之后:
- 飞雪桌面日历软件 V8.6 免费绿色版
软件名称: 飞雪桌面日历软件软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP / Win2008软件大小: 4MB图片预览: 软件简介: ...
- 菲菲更名宝贝 得意非凡版 v1.9 免费绿色版
软件名称: 菲菲更名宝贝 得意非凡版软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: 12.5MB图片预览: 软件简介:菲菲更名宝贝 ...
- SpringMVc上传excel或csv文件
1.JSP页面代码 <form enctype=""multipart/form-data" method="post"> <inp ...