本文主要展示了php发送get、post请求的6种方法的代码示例,分别为使用file_get_contents 、fopen、fsockopen、curl来发送GET和POST请求,代码如下:

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

 <?php
$url='http://www.jb51.net/';
$html = file_get_contents($url);
echo $html;
?>

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

 <?php
$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

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

 <?php
function get_url ($url,$cookie=false)
{
4 $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”;
15 $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;
33 } 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)
18 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扩展

 <?php
$ch = curl_init();
3 $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发送get、post请求的6种方法代码示例的更多相关文章

  1. php发送post请求的三种方法示例

    本文分享下php发送post请求的三种方法与示例代码,分别使用curl.file_get_content.fsocket来实现post提交数据,大家做个参考. php发送post请求的三种方法,分别使 ...

  2. axios发送两次请求原因及解决方法

    axios发送两次请求原因及解决方法 最近Vue项目中使用axios组件,在页面交互中发现axios会发送两次请求,一种请求方式为OPTIONS,另外一种为自己设置的. 如图: 什么是CORS通信? ...

  3. 【MySQL】锁——查看当前数据库锁请求的三种方法 20

    MySQL提供了查看当前数据库锁请求的三种方法:1. show  full  processlist命令  观察state和info列 2. show engine  innodb status\G ...

  4. IO流----操作文件的9种方法代码实现

    IO流----操作文件的9种方法代码实现: 1:使用字节流读写数据: 四种方式: method1:          每次读写一个字节,边读边写: /* * 复制文本文件. * * 数据源:从哪里来 ...

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

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

  6. ASP.NET MVC 实现AJAX跨域请求的两种方法

    通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据的加载,例如Google. 在ASP.NET MVC 框 ...

  7. 【转】Android下编译jni库的二种方法(含示例)

    原文网址:http://blog.sina.com.cn/s/blog_3e3fcadd01011384.html 总结如下:两种方法是:1)使用Android源码中的Make系统2)使用NDK(从N ...

  8. 【转】Android下编译jni库的二种方法(含示例) -- 不错

    原文网址:http://blog.sina.com.cn/s/blog_3e3fcadd01011384.html 总结如下:两种方法是:1)使用Android源码中的Make系统2)使用NDK(从N ...

  9. [转] Java 插入表记录后得到自增的id (附3种方法代码)

    转自:https://blog.csdn.net/yaerfeng/article/details/7231093 在MySQL中,使用auto_increment类型的id字段作为表的主键,并用它作 ...

随机推荐

  1. MySQL分布式集群之MyCAT(二)【转】

    在第一部分,有简单的介绍MyCAT的搭建和配置文件的基本情况,这一篇详细介绍schema的一些具体参数,以及实际作用        首先贴上自己测试用的schema文件,双引号之前的反斜杠不会消除,姑 ...

  2. Ubuntu 12.04下LVM2安装和操作实验

    实验环境: VirtualBox v4.3.20 Lubuntu 12.04LTS 前期准备: 1.添加虚拟盘:菜单"控制"->"设置"->&quo ...

  3. 33 Introducing the Go Race Detector

    Introducing the Go Race Detector 26 June 2013 Introduction Race conditions are among the most insidi ...

  4. HTTP Headers解析

    什么是HTTP Headers? 它包含了哪些内容? 利用requests.get()函数对豆瓣读书进行请求, 返回的r.headers如下所示: >>> import reques ...

  5. CF1030A 【In Search of an Easy Problem】

    题目巨简单,主要是给大家翻译一下 给n个数,其中存在1就输出HARD,否则输出EASY,不区分大小写 #include<iostream> #include<cstdio> u ...

  6. mongo体系架构学习

    MongoDB是一个可移植的数据库,它在流行的每一个平台上都可以使用,即所谓的跨平台性,在不同的操作系统上虽然略有差别,但是从整体架构上来看,MongoDB在不同的平台上是一样的,如数据逻辑结构和数据 ...

  7. 团体队列(UVa540)

    题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  8. linux添加桌面快捷方式

    linux通过软件中心安装的程序快捷图标都放在/usr/share/applications/目录下.点选一个图标右键,选择复制到,目录选择桌面即可.    但是有一些软件自行安装后,在那个目录里没有 ...

  9. pygame模块参数汇总(python游戏编程)

    一.HelloWorld pygame.init() #初始函数,使用pygame的第一步: pygame.display.set_mod((600,500),0,32) #生成主屏幕screen:第 ...

  10. Mac下思维导图Xmind使用入门

    1.软件下载 中文官网地址: http://www.xmindchina.net   安装过程比较傻瓜化,这里就不截图了. 2.用Xmind设计软件模块: 1>.新建一个思维导图,如下图,选 ...