Example code _php instance of PHP Curl simulate POST request and submit multidimensional array

Source: Internet
Author: User
Tags array to string curl

The following code to introduce the PHP Curl simulated POST request sample code, the following code:

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

<?php
if (isset ($_post[' name ')) {
 if (!empty ($_post[' name ')) {
 echo ' Hello, ', $_post[' name ']. ' ';
 }
}

Here's a description of the PHP Curl analog post submitted multidimensional array.

Today, we need to use curl to simulate post submission parameters, request a colleague to provide an interface, but in the parameters passed, there is a parameter of the value of the array, with a normal curl post code submitted, will report an error

PHP Notice:array to string conversion in/test/functions.php in line 30
Notice:array to string conversion in/test/functions.php on line 30

The code is as follows:

<?php
        $param = Array (
                ' uid ' => 123, 
                ' UIDs ' => Array (12,455), 
                ' Msgtype ' => ' with ',  
                ' Nick ' => ' AAA ',   
               );
        $url = "http://cx.com/t.php";
        Sending an interface request
        Senddatabycurl ($url, $param) via Curl post mode;
       A request to simulate a post via Curl
, function Senddatabycurl ($url, $data =array ()) {
  //to escape a space
  $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); Definition Timeout 3 seconds 
   //POST data
  curl_setopt ($ch, Curlopt_post, 1);
  Add the post variable to the
  curl_setopt ($ch, Curlopt_postfields, $data);
  Executes and obtains the content 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 the array without reporting PHP notice, the code is as follows:

 
Related Article

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.