无为清净楼资源网 Design By www.qnjia.com

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

<"http://www.cnblogs.com/test.php";//这里换成自己的服务器的地址
// 参数数组
$data = array (
 'name' => '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, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$return = curl_exec ( $ch );
curl_close ( $ch );
print_r($return);

2,远程服务器:

<"color: #ff0000">下面给大家介绍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

代码如下:

<"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,代码如下:

//通过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, http_build_query($data));  //所需传的数组用http_bulid_query()函数处理一下,就ok了
  //执行并获取url地址的内容
  $output = curl_exec($ch);
  $errorCode = curl_errno($ch);
  //释放curl句柄
  curl_close($ch);
  if(0 !== $errorCode) {
    return false;
  }
  return $output;
}
标签:
php_curl多维数组,curl_post多维数组,curl模拟post请求, php_curl_post请求

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?