php curl post提交数据

我也是第一次用 ,如果觉得写的不好,可以指出来(大家一起学习)!

需要将数组数据提交到http://wx.com/index.php/index/Test/index2

我这边方便测试直接添加到test数据库中,没刷新一次数据库新增一条数据(post提交数组成功)。

<?php
namespace app\index\controller;
use think\Config;
use think\Db;
use think\Controller;
use think\Request;

class Test extends controller
{
public function index()
{
$url = "http://wx.com/index.php/index/Test/index2";
$data = ['id'=>1,'info'=>'test','test'=>'123456'];
$res = $this->postResult($url, $data);
}
/**
* @$res curl提交数据成功,数据库新增数据
* @return [type] [description]
*/
public function index2()
{
$request = Request::instance();
$post = $request->param();
$res = [
// 'id' =>$post['id'],
'name' =>$post['info'],
'test' =>$post['test'],
];
$info = Db::name('test')->insert($res);
}

/**
* [postResult description]
* @param [type] string $url post的网址
* @param [type] array $data post的数据
* @return [type] resource 页面
*/
public function postResult($url, $data)
{
//初使化init方法
$ch = curl_init();
//指定URL
curl_setopt($ch, CURLOPT_URL, $url);
//设定请求后返回结果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//声明使用POST方式来进行发送
curl_setopt($ch, CURLOPT_POST, 1);
//发送什么数据呢
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//忽略证书
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//忽略header头信息
curl_setopt($ch, CURLOPT_HEADER, 0);
//设置超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
//发送请求
$output = curl_exec($ch);
//关闭curl
curl_close($ch);
//返回数据
return $output;

}
}

猜你喜欢

转载自www.cnblogs.com/wth9/p/9765412.html