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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?php

$uri = "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);

<?php $uri = "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,远程服务器:

?

1

2

3

4

5

6

<?php

if(isset($_POST['name'])){

 if(!empty($_POST['name'])){

 echo '您好,',$_POST['name'].'!';

 }

}

<?php if(isset($_POST['name'])){ if(!empty($_POST['name'])){ echo '您好,',$_POST['name'].'!'; } }

下面给大家介绍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

代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

//通过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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值