php curl example code to simulate post request and submit multidimensional array

Keywords: Programming curl PHP

The following code introduces the example code of php curl to simulate post request. The specific code is as follows:

$uri = "http://www.cnblogs.com/test.php "; / / here is the address of your own server
// Parameter array
$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. Remote server:

if(isset($_POST['name'])){
    if(!empty($_POST['name'])){
        echo 'Hello,',$_POST['name'].'!';
    }
}

Next, I will introduce the curl simulation post in php to submit multidimensional arrays.

Today, we need to use curl to simulate post submission parameters and request an interface provided by colleagues. However, among the parameters passed, there is an array of parameter values, which is submitted with ordinary curl post code, and 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

        $param = array(
                'uid' => 123, 
                'uids' => array(12,455), 
                'msgType' => 'WITH',  
                'nick' => 'aaa',   
               );
        $url = "http://cx.com/t.php";
        //Send interface request by post of curl
        SendDataByCurl($url,$param);
       //The request of post is simulated by curl;
function SendDataByCurl($url,$data=array()){
  //Escape spaces
  $url = str_replace(' ','+',$url);
  $ch = curl_init();
  //Set options, including URL s
  curl_setopt($ch, CURLOPT_URL, "$url");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch,CURLOPT_TIMEOUT,3); //Define timeout 3 seconds 
   // POST data
  curl_setopt($ch, CURLOPT_POST, 1);
  // Add the variable of post
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  //Execute and get the contents of the url address
  $output = curl_exec($ch);
  //Release curl handle
  curl_close($ch);
  return $output;
}

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

The array to be passed is processed with the HTTP build query() function as follows:

//The request of post is simulated by curl;
function SendDataByCurl($url,$data=array()){
  //Escape spaces
  $url = str_replace(' ','+',$url);
  $ch = curl_init();
  //Set options, including URL s
  curl_setopt($ch, CURLOPT_URL, "$url");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch,CURLOPT_TIMEOUT,3); //Define timeout 3 seconds 
   // POST data
  curl_setopt($ch, CURLOPT_POST, 1);
  // Add the variable of post
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));  //Use the HTTP build query() function to process the array to be passed, and it will be ok
  //Execute and get the contents of the url address
  $output = curl_exec($ch);
  $errorCode = curl_errno($ch);
  //Release curl handle
  curl_close($ch);
  if(0 !== $errorCode) {
    return false;
  }
  return $output;
}

 

Original link: https://www.jb51.net/article/75112.htm

 

Posted by debigmac on Fri, 13 Dec 2019 09:10:27 -0800