Sample code for php curl to simulate post requests and submit multi-dimensional arrays, curl sample code_PHP tutorial

WBOY
Release: 2016-07-12 09:04:29
Original
1414 people have browsed it

Sample code for php curl to simulate post requests and submit multi-dimensional arrays, curl sample code

The following code introduces to you the sample code for php curl to simulate post requests. The specific code is as follows:

<&#63;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);
Copy after login

2, Remote server:

<&#63;php
if(isset($_POST['name'])){
 if(!empty($_POST['name'])){
 echo '您好,',$_POST['name'].'!';
 }
}
Copy after login

The following will introduce curl to simulate post submission of multi-dimensional arrays in PHP.

Today, we need to use curl to simulate post submission parameters and request an interface provided by colleagues; however, among the parameters passed, the value of one parameter is an array. If submitted with ordinary curl post code, an error will be reported

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

The code is as follows:

<&#63;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;
}
Copy after login

After modifying the above code, you can complete the function of submitting the array without reporting a php notice. The code is as follows:

//通过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;
}
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1073124.htmlTechArticlephp curl simulates post request and submits a multi-dimensional array sample code, curl sample code The following code introduces php curl to you Sample code to simulate a post request, the specific code is as follows: php...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!