Phpcurl simulates post requests and submits multi-dimensional array sample code _ php instance

Source: Internet
Author: User
Tags array to string
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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.