Phpcurl simulates post requests and submits sample code for multi-dimensional arrays. curl sample code _ PHP Tutorial

Source: Internet
Author: User
Tags array to string curl sample
Phpcurl simulates the sample code of the post request and submits multi-dimensional arrays, and curl sample code. Phpcurl simulates post requests and submits sample code for multi-dimensional arrays. the following code describes the sample code of phpcurl simulating post requests. the code is as follows: php curl simulates the sample code of the post request and submits multi-dimensional arrays. the curl Sample code

The following code describes 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 ;}

Http://www.bkjia.com/PHPjc/1073124.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1073124.htmlTechArticlephp curl simulation post request and submit multi-dimensional array sample code, curl sample code below a piece of code to introduce you to php curl simulation post request sample code, the specific code is as follows: php...

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.