加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php curl模拟post请求和提交多维数组的示例代码

发布时间:2020-12-12 21:10:52 所属栏目:PHP教程 来源:网络整理
导读:下面一段代码给大家介绍php curl模拟post请求的示例代码,具体代码如下: 'tanteng'// 'password' => 'password');$ch = curl_init ();// print_r($ch);curl_setopt ( $ch,CURLOPT_URL,$uri );curl_setopt ( $ch,CURLOPT_POST,1 );curl_setopt ( $ch,CURLOPT

下面一段代码给大家介绍php curl模拟post请求的示例代码,具体代码如下:

'tanteng' // 'password' => 'password' ); $ch = curl_init (); // print_r($ch); curl_setopt ( $ch,CURLOPT_URL,$uri ); curl_setopt ( $ch,CURLOPT_POST,1 ); curl_setopt ( $ch,CURLOPT_HEADER,0 ); curl_setopt ( $ch,CURLOPT_RETURNTRANSFER,CURLOPT_POSTFIELDS,$data ); $return = curl_exec ( $ch ); curl_close ( $ch ); print_r($return);

2,远程服务器:

下面给大家介绍php中curl模拟post提交多维数组。

今天需要用curl模拟post提交参数,请求同事提供的一个接口;但是传递的参数中,有一个参数的值为数组,用普通的curl post代码提交,会报错误

PHP Notice: Array to string conversion in /test/functions.php on line 30 Notice: Array to string conversion in /test/functions.php on line 30

代码如下:

123,'uids' => array(12,455),'msgType' => 'WITH','nick' => 'aaa',); $url = "http://cx.com/t.php"; //通过curl的post方式发送接口请求 SendDataByCurl($url,$param); //通过curl模拟post的请求; function SendDataByCurl($url,$data=array()){ //对空格进行转义 $url = str_replace(' ','+',$url); $ch = curl_init(); //设置选项,包括URL curl_setopt($ch,"$url"); curl_setopt($ch,1); curl_setopt($ch,0); curl_setopt($ch,CURLOPT_TIMEOUT,3); //定义超时3秒钟 // POST数据 curl_setopt($ch,1); // 把post的变量加上 curl_setopt($ch,$data); //执行并获取url地址的内容 $output = curl_exec($ch); //释放curl句柄 curl_close($ch); return $output; }

经过修改上面代码,可以完成提交数组的功能,而不会报php notice,代码如下:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读