方法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

 //生成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

方法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. php发送post请求的三种方法示例

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

  2. php发送get、post请求的6种方法代码示例

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

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

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

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

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

  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. php发送post请求的三种方法

    引用:http://blog.sjzycxx.cn/post/435/ 1.使用 file_get_contents() /** * 发送post请求 * @param string $url 请求地 ...

  8. php发送get、post请求的6种方法简明总结

    方法1: 用file_get_contents 以get方式获取内容: ? 1 2 3 4 5 <?php $url='http://www.jb51.net/'; $html = file_g ...

  9. PHP发送HTTP请求的6种方法

    方法1: 用 file_get_contents 以get方式获取内容: <?php$url = 'https://wenda.shukaiming.com/';echo file_get_co ...

随机推荐

  1. Part 11 string functions in sql server

    Built in string functions in sql server 2008 LEFT, RIGHT, CHARINDEX and SUBSTRING functions in sql s ...

  2. easyui中Tab的tools按钮刷新当前tab

    easyui中Tab的tools按钮刷新当前tab 点击刷新按钮,刷新当前Tab选项卡. $('#index_tabs').tabs({ fit : true, border : false, too ...

  3. SQLServer触发器的使用

    创建: create trigger trigger_name on {table_name view_name} {for After Instead of } [ insert, update,d ...

  4. 通过SQL ID查询SQL Text

    SELECT SQL_ID, SQL_TEXT,FIRST_LOAD_TIME, EXECUTIONS FROM V$SQLAREA where SQL_ID='22v8fyk0juw25';

  5. 第八篇、封装NSURLSession网络请求框架

    主要功能介绍: 1.GET请求操作 2.POST请求操作 1.处理params参数(例如拼接成:usename="123"&password="123" ...

  6. 点击文字label同时选中checkbox radio

    在做网页的时候一般会有一个需求:点击一段文字信息的同时选中某个checkbox 旧处理方式是在这段文字上加上点击事件触发checkbox的选中事件 //jq中://选中 $("#ID&quo ...

  7. 33选6算法:M个数N个为一组,无重复的排列组合

    private void button1_Click(object sender, EventArgs e) { int nCnt = 0; List nNumList = new List(); f ...

  8. WPF 绑定五(本身就是数据源)

    xaml: <Window x:Class="WpfApplication1.Window5" xmlns="http://schemas.microsoft.co ...

  9. linux服务器修改ftp默认21端口方法

    1.登录服务器,打开vsftp.conf文件 # vim /etc/vsftpd/vsftpd.conf 2.在文件末尾增加listen_port=8021 #remote_charset=CP125 ...

  10. Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout

    如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...