php使用file_get_contents 或者curl 发送get/post 请求 的方法总结
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 请求 的方法总结的更多相关文章
- 利用postman进行接口测试并发送带cookie请求的方法
		做web测试的基本上都用用到postman去做一些接口测试,比如测试接口的访问权限,对于某些接口用户A可以访问,用户B不能访问:比如有时需要读取文件的数据.在postman上要实现这样测试,我们就必要 ... 
- php CURL 发送get,post请求
		// 发送一个get请求 $url 发送地址 function get($url) { //初始化操作 $curl = curl_init($url); ... 
- php 中使用cURL发送get/post请求,上传图片,批处理
		cURL是利用url语法规定传输文件和数据的工具.php中有curl拓展,一般用来实现网络抓取,模拟发送get post请求,文件上传. 在php中建立curl的基本步骤如下: 1 初始化 ... 
- curl 发送get post请求
		function getAction($url=''){ // curl 请求一共分四步,初始化,设置属性,执行并获取结果,释放句柄 // 一.初始化 $curl = curl_init(); // ... 
- php curl发送post get请求
		POST: function curl_post_https($url, $data, $header){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个CURL会话 ... 
- linux shell中curl 发送post请求json格式问题
		今天在linux中使用curl发送一个post请求时,带有json的数据,在发送时发现json中的变量没有解析出来 如下 curl -i -X POST -H 'Content-type':'appl ... 
- 发送POST测试请求的若干方法
		最近在工作中需要测试发送带Json格式body值的HTTP POST请求.起初,我在Linux环境下使用curl命令去发送请求,但是,在发送的过程中却遇到了一些问题,经过一段时间的摸索,发现了以下几种 ... 
- Requests发送带cookies请求
		一.缘 起 最近学习[悠悠课堂]的接口自动化教程,文中提到Requests发送带cookies请求的方法,笔者随之也将其用于手头实际项目中,大致如下 二.背 景 实际需求是监控平台侧下发消息有无异常, ... 
- 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 ... 
随机推荐
- Ubuntu 查找文件夹中内容包含关键字的文件,路径为当前文件夹
			From CSDN http://blog.csdn.net/lizhenmingdirk/article/details/44834997 grep -rl "keyword" ... 
- windows RT开发笔记:WinRT DLL及其调用研究
			一. 几个概念: WinRT : Windows Runtime, windows运行时.创建Windows运行时(WinRT)是为了在Windows上给用户提供一种流畅且安全的应用体验.WinRT会 ... 
- windows常用命令集锦
			开始→运行→输入的命令集锦 gpedit.msc-----组策略 sndrec32-------录音机 Nslookup-------IP地址侦测器 explorer-------打开资源管理器 lo ... 
- koa2获取用户ip
			调用下面方法即可获取 // koa2 中 req 为 ctx.req const getUserIp = (req) => { return req.headers['x-forwarded-f ... 
- ThinkPHP5.0版本的优势在于:
			更灵活的路由: 依赖注入: 请求缓存: 更强大的查询语法: 引入了请求/响应对象: 路由地址反解生成: 增强的模型功能: API开发友好: 改进的异常机制: 远程调试支持: 单元测试支持: 命令行工具 ... 
- February 24 2017 Week 8 Friday
			If you fail, don't forget to learn your lesson. 如果你失败了,千万别忘了汲取教训. Frankly speaking, it is easy to ta ... 
- dos基础+环境搭建基础理论
			dos基础 市面上两大操作系统 windows.*nix(unix.linux.mac.bsd(安全性比较高)) 后三种都属于unix的衍生版本 linux是为了兼容unix开发的,最后开放了源代码 ... 
- win8中 用office 提示值不在预期的范围内
			原文:http://bbs.kafan.cn/thread-1401951-1-1.htmlhttp://bbs.kafan.cn/thread-1401951-1-1.html 错误如下: 名称: ... 
- 一种不通过UI给C4C自定义BO创建测试数据的方式
			假设我在Cloud Studio里创建了如下一个非常简单的自定义BO: 我想生成一些该BO的实例.以前我采用的做法是给这个自定义BO创建编辑用的UI.然后使用这些UI创建BO实例.这种方式很花费时间. ... 
- appendChild与Transition动画
			在createElement之后,直接把这个div append到body中,是不会触发css3 transition动画的 必须要让浏览器计算div的css属性后,然后再设置div的style,才会 ... 
