分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

php curl post提交数据

发布时间:2023-09-06 02:17责任编辑:白小东关键词:url

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

需要将数组数据提交到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;

}
}

php curl post提交数据

原文地址:https://www.cnblogs.com/wth9/p/9765412.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved