我也是第一次用 ,如果覺得寫的不好,可以指出來(大家一起學習)!
需要將數組數據提交到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;
}
}