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. Ruby系列文章之1---开发者应该熟悉的10个工具

    1. Git Git是进入Ruby这个生态圈首先最应该学会的工具.几乎所有以Ruby开发出来的套件都放在Github上.也就是不管你要下载或修改协作都需要透过Git. 2. RVM Ruby有很多种i ...

  2. cropperjs的高度过大(container height too much)

    cropperjs的高度过大(container height too much) 标签(空格分隔): JavaScript 业务需要web头像裁切,用canvas写了个demo卡成一匹马,于是就去寻 ...

  3. VSCode-python 进阶配置

    VSCode-python 进阶配置 中文乱码 中文乱码,网上一堆解决方法,但是根本没有有效起作用的. 在python脚本的前面添加: # -*- coding:utf-8 -*- 并不能在控制台输出 ...

  4. 48. Rotate Image (matrix retation, transpose) Amazon problem

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. 如何用python语言撸出图表系统

    公司指标图表化显示,解决目前跟踪技术指标数据的各种不方便:于是话不多说,撸起袖子就是干: 1.挖掘需求和罗列功能点: a.图表显示技术指标数据. b.根据服务名和系统名查询对应的图表. c.根据日期区 ...

  6. NO.006-2018.02.11《卜算子·我住长江头》宋代:李之仪

    卜算子·我住长江头_古诗文网(bǔ) 卜算子·我住长江头 宋代:李之仪 我住长江头,君住长江尾.日日思君不见君,共饮长江水. 我居住在长江上游,你居住在长江下游. 天天想念你却见不到你,共同喝着长江的 ...

  7. mobile easyui兼容实体数据(tree插件为例)

    ORM的实体类和数据库的类是一一对应的,如果有多级的嵌套循环json返回到前台为了方便展示可以使用mobile easyui,但是mobile easyui又需要特定的属性才可以,比如id,text, ...

  8. Android4.4 ContentResolver查询图片无效 及 图库删除 添加图片后,ContentResolver不更新的问题解决

    问题背景: 參考链接 做了一个图片浏览,用ContentResolver扫描图库照片.且严格依照时间拍摄顺序排好序显示在listview里.例如以下图所看到的: watermark/2/text/aH ...

  9. 【iOS】那些年,遇到的小坑

    'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithObjectsAndKeys:]: second ...

  10. P3393 逃离僵尸岛

    P3393 逃离僵尸岛 啊.好久不写dij手都生了 这道题就是预先处理出是否是危险城市,然后跑一个最短路就行了 然后因为我感觉这个对时间要求不大紧.判断危险城市时就写了个电风扇(DFS) 然后T飞了呜 ...