This article mainly introduces the sample code of phpcurl simulating post requests and submitting multi-dimensional arrays. If you need it, refer to the following code to introduce the sample code of php curl simulating post requests, the Code is as follows:
<? Php $ uri = "http://www.cnblogs.com/test.php "; // replace this with the address of your 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:
<? Phpif (isset ($ _ POST ['name']) {if (! Empty ($ _ POST ['name']) {echo 'hello, ', $ _ POST ['name'].'! ';}}
The following describes how to submit multi-dimensional arrays by using curl in php to simulate post.
Today, you need to use curl to simulate the post request parameter and request an interface provided by a colleague. However, in the passed parameter, the value of a parameter is an array and is submitted using the common 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:
<? Php $ param = array ('uid' => 123, 'uid' => array (12,455), 'msgtype' => 'with ', 'Nick '=> 'aaa',); $ url = "http://cx.com/t.php"; // send the Interface request SendDataByCurl ($ url, $ param) through the curl post method ); // simulate post requests through curl; function SendDataByCurl ($ url, $ data = array () {// escape spaces $ url = str_replace ('', '+', $ url); $ ch = curl_init (); // set options, including 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); // defines timeout for 3 seconds // curl_setopt ($ ch, CURLOPT_POST, 1); // Add the post variable to curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data ); // execute and obtain the url content $ output = curl_exec ($ ch); // release the curl handle curl_close ($ ch); return $ output ;}
After modifying the code above, you can complete the array submission function without reporting php notice. The Code is as follows:
// Simulate post requests through curl; function SendDataByCurl ($ url, $ data = array () {// escape spaces $ url = str_replace ('', '+', $ url); $ ch = curl_init (); // set options, including 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); // defines timeout for 3 seconds // curl_setopt ($ ch, CURLOPT_POST, 1); // Add the post variable to curl_setopt ($ ch, CURLOPT_POSTFIELDS, Http_build_query ($ data); // use the http_bustm_query () function to process the expected array, then OK // execute and get the url content $ output = curl_exec ($ ch); $ errorCode = curl_errno ($ ch ); // release the curl handle curl_close ($ ch); if (0! ==$ ErrorCode) {return false;} return $ output ;}