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

代码如下:

<?php                $param = array(                                'uid' => 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, CURLOPT_URL, "$url");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_TIMEOUT,3);  //定义超时3秒钟  
     // POST数据
    curl_setopt($ch, CURLOPT_POST, 1);    // 把post的变量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
    //执行并获取url地址的内容
    $output = curl_exec($ch);    //释放curl句柄
    curl_close($ch);    return $output;

}

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

 

}

本文转自 陈小龙哈 51CTO博客,原文链接:http://blog.51cto.com/chenxiaolong/1733550


相关文章
|
4月前
|
JSON PHP 数据格式
PHP curl方法封装
PHP curl方法封装
31 0
|
1月前
|
PHP 数据格式
PHP 中的CURL 模拟表单的post提交
PHP 中的CURL 模拟表单的post提交
16 0
|
2月前
|
安全 PHP 数据库
从建站打拿站 -- PHP(GET和POST)
从建站打拿站 -- PHP(GET和POST)
11 0
|
7月前
|
PHP
PHP $_POST
PHP $_POST
28 0
|
4月前
|
JSON 前端开发 PHP
dataType:'json'时post,总是调用error???附带php和json的一些坑
ajax是一个不错的东西 当dataType:'json'时提交,总是返回error 这是为什么? 经过折腾,终于发现因为要求的是json,返回的信息并不是json格式,因此总是出错
34 0
|
8月前
|
JSON PHP 数据格式
PHP curl方法封装
PHP curl方法封装
65 0
|
9月前
|
JavaScript API PHP
php和js获取POST参数API接口数据的不同解决方案
php和js获取POST参数API接口数据的不同解决方案
80 0
|
9月前
|
PHP
php函数file_get_contents无法获取到https链接内容问题使用curl的解决方案
php函数file_get_contents无法获取到https链接内容问题使用curl的解决方案
111 0
|
9月前
|
前端开发 PHP
php解决ajax使用post请求时提交的数据过多而导致et::ERR_CONNECTION_RESET的解决方案
php解决ajax使用post请求时提交的数据过多而导致et::ERR_CONNECTION_RESET的解决方案
131 0