PHP Curl simulates a POST request and submits a multidimensional array of sample code, Curl sample Code _php tutorial

Source: Internet
Author: User
Tags array to string curl sample

PHP Curl simulates a POST request and submits a multidimensional array of sample code, Curl sample code


The following code shows you the sample code for the PHP Curl mock-up POST request, as follows:

<?php$uri = "http://www.cnblogs.com/test.php";//This is replaced by 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, C Urlopt_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);p rint_r ($return);

2, remote server:

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

Here is an introduction to PHP in the Curl analog post submission multidimensional array.

Today, we need to simulate the post submission parameters with Curl and request an interface provided by a colleague, but the passed parameter has an array of values for the parameter, submitted with the normal Curl post code, and reported an error

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,                 ' uids ' = = Array (12,455),                 ' msgtype ' = ' with ',                  ' Nick ' = ' aaa ',                  );        $url = "http://cx.com/t.php";        Send interface request via Curl Post        senddatabycurl ($url, $param);       A request to simulate a post via curl; function Senddatabycurl ($url, $data =array ()) {  //escapes the 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 variables to  curl_setopt ($ch, Curlopt_postfields, $data);  Executes and obtains 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 the array, but not the PHP notice, the code is as follows:

A request to simulate a post via curl; function Senddatabycurl ($url, $data =array ()) {  //escapes the 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 variables to  curl_setopt ($ch, Curlopt_postfields, Http_build_query ($data));  The required array is processed with the http_bulid_query () function, 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;}

http://www.bkjia.com/PHPjc/1073124.html www.bkjia.com true http://www.bkjia.com/PHPjc/1073124.html techarticle PHP Curl simulates a POST request and submits a multidimensional array of sample code, the Curl sample code below shows you the sample code for the PHP Curl Mock POST request, as follows: Php ...

  • 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.