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

$url='http://www.jb51.net/';

$html = file\_get\_contents($url);

echo $html;

?>

方法2: 用fopen打开url, 以get方式获取内容:

$fp = fopen($url, ‘r');

stream\_get\_meta_data($fp);

while(!feof($fp)) {

$result .= fgets($fp, 1024);

}

echo “url body: $result”;

fclose($fp);

?>

方法3:用file_get_contents函数,以post方式获取url

$data = array (‘foo' => ‘bar');

$data = http\_build\_query($data);

$opts = array (

‘http' => array (

‘method' => ‘POST',

‘header'=> “Content-type: application/x-www-form-urlencodedrn” .

“Content-Length: ” . strlen($data) . “rn”,

‘content' => $data

)

);

$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 选项开启

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.1rn”;

$request .= “Host: $url\[host\]rn”;

$request .= “Connection: Closern”;

if($cookie) $request.=”Cookie: $cookien”;

$request.=”rn”;

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,”rnrn”);

$body=substr($body,4,strlen($body));

return $body;

}

return false;

}

?>

方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

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.1n”;

$request.=”Host: “.$URL_Info\["host"\].”n”;

$request.=”Referer: $referern”;

$request.=”Content-type: application/x-www-form-urlencodedn”;

$request.=”Content-length: “.strlen($data_string).”n”;

$request.=”Connection: closen”;

$request.=”Cookie: $cookien”;

$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扩展

$ch = curl_init();

$timeout = 5;

curl\_setopt ($ch, CURLOPT\_URL, ‘http://www.jb51.net/');

curl\_setopt ($ch, CURLOPT\_RETURNTRANSFER, 1);

curl\_setopt ($ch, CURLOPT\_CONNECTTIMEOUT, $timeout);

$file\_contents = curl\_exec($ch);

curl_close($ch);

echo $file_contents;

?>

php is_post,PHP发送get、post请求的6种方法简明总结相关推荐

  1. java发送get请求_java发送http get请求的两种方法(总结)

    长话短说,废话不说 一.第一种方式,通过HttpClient方式,代码如下: public static String httpGet(String url, String charset) thro ...

  2. php发送post请求的三种方法

    这篇文章主要介绍了php发送post请求的三种方法,分别使用curl.file_get_content.fsocket来实现post提交数据,需要的朋友可以参考下 class Request{ pub ...

  3. http 请求的7 种方法

    http 请求的7 种方法 1.get 2.post 3.put 4.delete 5.head 6.trace 7.opinions head 请求和 get请求类似,但是服务器只响应首部,不会返回 ...

  4. java get请求 参数_HttpServletRequest获取GET请求参数5种方法

    首页 > Java Web > SpringMvc应用 > HttpServletRequest HttpServletRequest获取GET请求参数5种方法 HttpServle ...

  5. java发送http get请求的两种方式

    长话短说,废话不说 一.第一种方式,通过HttpClient方式,代码如下: public static String httpGet(String url, String charset)throw ...

  6. Ajax向后端发送PUT请求的两种方法

    文章目录 一.将POST请求修改为PUT请求 二.直接发送PUT请求 一.将POST请求修改为PUT请求 通过HiddenHttpMethodFilter过滤器将POST请求修改为PUT请求 需要传入 ...

  7. Python接口测试- requests 发送 post 请求的几种方法

    目录 前言: 发送 json 格式的数据 前言:  想了解更多相关知识请关注我吧!或者点击这里领取全套[软件测试/自动化测试]海量资料免费领取 POST 请求用于向服务器发送数据.与 get 相比更安 ...

  8. 解决跨域请求的四种方法

    跨域 跨域就是请求的url中的"协议"."域名"."端口号"其中任何一种不一样都是属于跨域.解决跨域的主要的四种方法是jsonp.跨域资源共 ...

  9. QTcpSocket 发送和接收数据的几种方法

    QTcpSocket  发送数据的几种方法 1.QTcpSocket 继承于QAbstractSocket继承于QIODevice 2.QTcpSocket 提供的几种接收和发送数据方法 write  ...

最新文章

  1. 现在的Android程序员为什么会感到焦虑?焦虑的源头在哪里?该怎么去缓解焦虑呢?——没有无中生有的贩卖焦虑,只有你的挣扎和不甘。
  2. 如何解决 SQL Server 2000 中的连接问题
  3. 【luogu 3375】【模板】KMP字符串匹配
  4. 搭建Linux0.11系统环境
  5. 开源项目管理工具禅道ZenTaoPMS发布0.6 beta版本
  6. STL 中的链表排序
  7. C标准函数库中获取时间与日期、对时间与日期数据操作及格式化
  8. [导入]用事件和异常返回多种结果
  9. ReultSet有什么作用和使用
  10. Tensorflow CIFAR10 (二分类)
  11. Unity发布Android时需要的Android SDK/NDK的下载
  12. 用计算机如何算行列式,数学计算器使用操作步骤
  13. extjs 教程 java_ExtJS实战 01——HelloWorld
  14. 戴尔服务器引导盘装2008,DELL R720服务器安装Windows Server 2008 R2系统的图文详解
  15. 索迪斯携手喜茶致敬白衣天使、慰问抗疫英雄
  16. 本周上榜的这9本原创技术书很赞,《数学之美》作者吴军博士新书霸榜
  17. uniapp 微信小程序登录方法封装
  18. html收藏夹导入mac,Mac浏览器导入其他浏览器收藏-功能说明
  19. 机器学习之随机森林填补缺失值和众数填补缺失值
  20. 普通母函数模板—hdu1028

热门文章

  1. javascript控制html高,Javascript可以控制css吗?
  2. arcgis python教程视频_arcgispython教程
  3. latex安装包_LaTeX排版入门须知
  4. 精选30张炫酷的动态交互式图表,Pandas一键生成,通俗易懂
  5. Python 面向监狱编程,就靠它了
  6. 力荐!Python的14张思维导图 | 附下载方式
  7. 肝!一行 Python 代码实现并行
  8. 美团算法专家:入门机器学习,比你想的要简单
  9. 【干货】TensorFlow 2.0官方风格与设计模式指南(附示例代码)
  10. QT QNetworkInterface::allAddresses();获取了很多无效的地址_Qt编写地图综合应用16-省市轮廓图下载...