thinkphp5.0 CURL用post请求接口数据
//测试 请求接口
public function index(){
$arr = array('a'=>'555','b'=>56454564);
$data=$this->post_json_data('http://www.test.com/public/index/api/postTest',json_encode($arr));
dump(json_decode($data['result'],true));
} //测试 接口
public function postTest(){
//显示获得的数据
if($this->request->isPost()){
$arr = array('a'=>'666666','b'=>999999);
return json_encode($arr);
} }
/*
* post 发送JSON 格式数据
* @param $url string URL
* @param $data_string string 请求的具体内容
* @return array
* code 状态码
* result 返回结果
*/
function post_json_data($url, $data_string) {
//初始化
$ch = curl_init();
//设置post方式提交
curl_setopt($ch, CURLOPT_POST, 1);
//设置抓取的url
curl_setopt($ch, CURLOPT_URL, $url);
//设置post数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
//设置头文件的信息作为数据流输出
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
ob_start();
//执行命令
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array('code'=>$return_code, 'result'=>$return_content);
}
个人记录一下哈
thinkphp5.0 CURL用post请求接口数据的更多相关文章
- php curl模拟post请求提交数据样例总结
在php中要模拟post请求数据提交我们会使用到curl函数,以下我来给大家举几个curl模拟post请求提交数据样例有须要的朋友可參考參考.注意:curl函数在php中默认是不被支持的,假设须要使用 ...
- python UI自动化实战记录二:请求接口数据并提取数据
该部分记录如何获取预期结果-接口响应数据,分成两步: 1 获取数据源接口数据 2 提取后续页面对比中要用到的数据 并且为了便于后续调用,将接口相关的都封装到ProjectApi类中. 新建python ...
- php curl模拟post请求提交数据例子总结
php curl模拟post请求提交数据例子总结 [导读] 在php中要模拟post请求数据提交我们会使用到curl函数,下面我来给大家举几个curl模拟post请求提交数据例子有需要的朋友可参考参考 ...
- Thinkphp3.2.3框架下封装公共的函数,例如封装CURL函数来获取接口数据
当我们需要在控制层调用相同的封装函数时,写多次相同的函数,显得代码十分的拉杂,不精简: TP框架有一个很好的机制,可以再Common定义一个function.php函数,当我们在控制层调用的时候直接调 ...
- Thinkphp5.0 的使用模型Model删除数据
Thinkphp5.0 的使用模型Model删除数据 一.使用destory()删除数据 //删除id为3的记录 $res = User::destroy(3); //返回影响的行数 dump($re ...
- Thinkphp5.0 的使用模型Model更新数据
Thinkphp5.0 的使用模型Model更新数据 (1)使用update()方法进行更新数据 一.where条件写在更新数据中 (这种情况更新的数据,必须含主键) $res = User::upd ...
- Thinkphp5.0 的使用模型Model添加数据
Thinkphp5.0 的使用模型Model添加数据 使用create()方法添加数据 $res = TestUser::create([ 'name' => 'zhao liu', 'pass ...
- thinkphp5.0极速搭建restful风格接口层实例
作为国内最流行的php框架thinkphp,很快就会发布v5.0正式版了,现在还是rc4版本,但已经很强大了下面是基于ThinkPHP V5.0 RC4框架,以restful风格完成的新闻查询(get ...
- C# Http请求接口数据的两种方式Get and Post
面向接口编程是一种设计思想,无论用什么语言都少不了面向接口开发思想,在软件开发过程中,常常要调用接口,接下来就是介绍C#调用其它开发商提供的接口进行获取数据,http接口方式获取接口数据. Get请求 ...
随机推荐
- HITS
HITS 1 概述 HITS(hypertext induced topic search)超链接归纳主题搜索是由kleinbers在90年代提出的基于链接分析的网页排名算法.Hits算法是利用Hub ...
- Web图片编辑控件开发文档-Xproer.ImageEditor
版权所有 2009-2014 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com 产品首页:http://www.ncmem.com/webplug/image-e ...
- 团队-Forward-团队一阶段互评
学号:2015035107105得分:4原因:代码不规范,有一些错误,需要我们的帮助. 学号:2015035107109得分:7原因:与队员沟通少,代码衔接有问题. 学号:2015035107113得 ...
- Elasticsearch 安装的时候,Unsupported major.minor version 51.0问题的解决
Elasticsearch安装的时候报错 bootstrap/Elasticsearch : Unsupported major.minor version 51.0 网上一般说的方法是,升级或者降级 ...
- nancy中的身份验证
在nancy中,身份验证分为basic ,form ,token,stateless几种,basic和form这里不说了,其中如果是使用cookie来保存身份的, 需要注意的是:cookie有大小的限 ...
- Git Note - git tag
git tag is used to create labels, usually for version numbers. Format: git tag <TagName> <r ...
- Default style sheet for HTML 4
http://www.w3.org/TR/CSS21/sample.html html, address, blockquote, body, dd, div, dl, dt, fieldset, f ...
- solr特点三: boost(改变默认打分排序)
有时候默认的字段打分不能满足我们的需要,如我们想把关键词出现在标题中的优先显示. 测试于:Solr 4.5.1, Jdk 1.6.0_45, Tomcat 6.0.37 | CentOS 5.7 实现 ...
- Sql里时间加减
简单的时间加减 DATEADD(dd,-30, GETDATE())) 使用DateADD方法: 参数1:间隔,表示要添加的时间间隔,一天还是一月还是一年 参数2:要加或减的个数,加一年或加一月 参数 ...
- LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)
http://lightoj.com/volume_showproblem.php?problem=1213 Fantasy of a Summation Time Limit:2000MS ...