file_get_contents模拟GET/POST请求

模拟GET请求:

<?php
$data = array(
'name'=>'zhezhao',
'age'=>'23'
);
$query = http_build_query($data); $url = 'http://localhost/get.php';//这里一定要写完整的服务页面地址,否则php程序不会运行 $result = file_get_contents($url.'?'.$query); echo $result;

模拟POST请求:

<?php
$data = array(
'name'=>'zhezhao',
'age'=>23
); $query = http_build_query($data); $options['http'] = array(
'timeout'=>60,
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $query
); $url = "http://localhost/post.php";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context); echo $result;
?>

curl模拟GET/POST请求

GET请求的参数

get传递参数和正常请求url传递参数的方式一样

function get_info($card){

    $url ="http://www.sdt.com/api/White/CardInfo?cardNo=".$bank_card; 

    $ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0); //执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
return $output;
}

HTTPS请求时要注意SSL验证

function get_bankcard_info($bank_card){

    $url ="https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=".$bank_card."&cardBinCheck=true";

    $ch = curl_init();

    //设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //执行并获取HTML文档内容
$output = curl_exec($ch); //释放curl句柄
curl_close($ch);
return $output;
}

post请求

/**
* 模拟post进行url请求
* @param string $url
* @param array $param
*/
function request_post($url = '', $param = []) {
if (empty($url) || empty($param)) {
return false;
}
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
$postUrl = $url;
$curlPost = $param;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch); return $data;
}

 

php使用file_get_contents 或者curl 发送get/post 请求 的方法总结的更多相关文章

  1. 利用postman进行接口测试并发送带cookie请求的方法

    做web测试的基本上都用用到postman去做一些接口测试,比如测试接口的访问权限,对于某些接口用户A可以访问,用户B不能访问:比如有时需要读取文件的数据.在postman上要实现这样测试,我们就必要 ...

  2. php CURL 发送get,post请求

    // 发送一个get请求 $url 发送地址    function get($url)    {        //初始化操作        $curl = curl_init($url);     ...

  3. php 中使用cURL发送get/post请求,上传图片,批处理

    cURL是利用url语法规定传输文件和数据的工具.php中有curl拓展,一般用来实现网络抓取,模拟发送get   post请求,文件上传. 在php中建立curl的基本步骤如下: 1 初始化     ...

  4. curl 发送get post请求

    function getAction($url=''){ // curl 请求一共分四步,初始化,设置属性,执行并获取结果,释放句柄 // 一.初始化 $curl = curl_init(); // ...

  5. php curl发送post get请求

    POST: function curl_post_https($url, $data, $header){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个CURL会话 ...

  6. linux shell中curl 发送post请求json格式问题

    今天在linux中使用curl发送一个post请求时,带有json的数据,在发送时发现json中的变量没有解析出来 如下 curl -i -X POST -H 'Content-type':'appl ...

  7. 发送POST测试请求的若干方法

    最近在工作中需要测试发送带Json格式body值的HTTP POST请求.起初,我在Linux环境下使用curl命令去发送请求,但是,在发送的过程中却遇到了一些问题,经过一段时间的摸索,发现了以下几种 ...

  8. Requests发送带cookies请求

    一.缘 起 最近学习[悠悠课堂]的接口自动化教程,文中提到Requests发送带cookies请求的方法,笔者随之也将其用于手头实际项目中,大致如下 二.背 景 实际需求是监控平台侧下发消息有无异常, ...

  9. nginx+fastcgi php 使用file_get_contents、curl、fopen读取localhost本站点.php异常的情况

    原文:http://www.oicto.com/nginx_fastcgi_php_file_get_contents/ 参考:http://os.51cto.com/art/201408/44920 ...

随机推荐

  1. 2017年Nature文章“Millions of online book co-purchases reveal partisan differences in the consumption of science”阅读笔记

    论文:      Millions of online book co-purchases reveal partisan differences in the consumption of scie ...

  2. SQL Server 的 主键 解决方案 NEWID() , 自增ID

    在 SQL Server 表的主键有自增Id ,和  GUID. 1.  自增Id 优点:索引空间小,索引连续.在大量数据插入的时候性能有特别大的优势. 缺点:可移植性差,在数据迁移的时候. 2. G ...

  3. vue项目运行报错:Module bulid failed: Error: Node Sass does not yet support your current environment

    出错起因:      从GitLab clone项目 --> 用 npm install 命令下载依赖包 --> #npm run dev,报错 错误截图: 解决方法:   思路:单独 i ...

  4. Oracle10g使用$ORACLE_HOME/rdbms/admin/awrrpt.sql报错

    Enter value for report_name: Using the report name awrrpt_1_591_593.htmlselect output from table(dbm ...

  5. 12C RAC 常用检查命令,持续总结中

    grid: olsnodes -s列出集群中节点crsctl check cluster -all检查几圈状态crsctl check clustercrsctl check crs 检查当前节点sr ...

  6. 获取v$latch数据源实验

    实验环境:Oracle Rac 11.2.0.3 首先获取v$latch的定义:通过PL/SQL或者get ddl等常规途径只能获取到v_$latch相关的视图信息.需要通过特殊方法获取v$latch ...

  7. 长大Tips

    队名:CW 队员: B20150304403 王香辉 B20150304408 李孟君 B20150304411 曾翡 B20150304414 吴海波 B20150304430 文淼 B201503 ...

  8. python:正则模块

    1,正则表达式 正则表达式是用来做字符串的匹配的,正则有他自己的规则,和python没有关系,一种匹配字符串的规则. 2,字符组 在同一个位置可能出现的各种字符组成了一个字符组,在正则表达式中用[]表 ...

  9. 写在归程路上——2018ROBOCUP机器人世界杯中国赛

    写入自强队名单一年,认识Daniel十个月,第二次代表上海大学参加国赛,这一次是在浙江绍兴.坐在返沪的车上,心里担心着作业和明天的早课,写这篇博文来打发打发时间. ROBOCUP两个半比赛日,项目主要 ...

  10. c语言描述的链队列的基本操作

    #include<stdio.h> #include<stdlib.h> #define ok 0 #define error 1 //链队列特点在于不仅有链的头指针和尾指针, ...