PHP Use the Curl function to send a POST request considerations _php Tips

Source: Internet
Author: User
Tags arrays curl json strlen

Objective

A few days ago in the work encountered a demand, when the user clicked a button, the background to initiate a POST request to me, I accept the data passed over. At the outset, colleagues were sent in a common package of corporate frameworks, with the http_request() following code:

 Public Function Http_request ($url, $post = ", $timeout = 5) { 
 if (empty ($url)) {return
  ;
 }
 $ch = Curl_init ();
 curl_setopt ($ch, Curlopt_url, $url);
 curl_setopt ($ch, Curlopt_header, 0);
 curl_setopt ($ch, Curlopt_returntransfer, 1);
 curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
 curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE);
 
 if ($post!= ' &&!empty ($post)) {
  curl_setopt ($ch, Curlopt_post, 1);
  curl_setopt ($ch, Curlopt_postfields, $post);
  curl_setopt ($ch, Curlopt_httpheader, Array (' Content-type:application/json ', ' content-length: '. strlen ($post)));
 }
 curl_setopt ($ch, Curlopt_timeout, $timeout);
 $result = curl_exec ($ch);
 Curl_close ($ch);
 return $result;
 }

At first I did not notice that the data passed was application/json encoded JSON string, I was in the background directly with the application/x-www-form-urlencoded data to accept the code format to pass through the data (is the direct use of the $_POST way to get), the result of course nothing has been taken. Later, colleagues directly to change the http_request() method, directly pass application/x-www-form-urlencoded the coded format of the data, I did not make changes.

For the above question, I have been wondering why did not get the data passed over.

Today the project was basically completed and studied below.

When a function in PHP curl() makes a POST request, the format of the data can be passed in the following ways:

(1): A key=>value key value pair string that is spliced by a parameter. form as follows:name=xxx&age=23$sex=1

This request parameter is encoded according application/x-www-form-urlencoded to the default.

(2): An array of key values consisting of parameters key=>value (only one-dimensional arrays, and arrays of higher dimensions will complain).

form as follows:

  [name= "xxx", age =%, sex = male]

This request parameter is encoded by default in a multipart/form-data format.

It says that curl() when you make a POST request, you can only pass a one-dimensional array as a passing parameter, so what do you do if you want to pass a multidimensional array?

There are two ways to deal with it, respectively, in the following way 3 and Mode 4.

(3): The multidimensional array is http_build_query() processed to wait until a string of Key=>value key value pairs is formatted.

As shown in the following:

$data = [
          "msg" => "This is a test data",
          "xxx" => "yyyy", "
          msg_data" => [
                          "name" => "Sunms",
                          "age" =>23, "
                          sex" => "male",
                          "content" =>[
                                 1,2,3
                                ]
                       ]
       ;

The following string is obtained:

Msg= This is a test data &xxx=yyyy&msg_data[name]=sunms&msg_data[age]=23&msg_data[sex]= male &msg_data[ Content][0]=1&msg_data[content][1]=2&msg_data[content][2]=3

This method is also application/x-www-form-urlencoded进 encoded by the line, where the receiver can be directly obtained by $_post.

(4): Converts a multidimensional array into a JSON-formatted string, formats the string, application/json obtains the passed-through JSON-formatted string in the receiver's file_get_contents(“php://input”) or $GLOBALS[‘HTTP_RAW_POST_DATA'] way, and then converts the string in JSON format to an array for processing.

$data = [];
$data _string = Json_encode ($data);
.....
Set Header information
curl_setopt ($ch, Curlopt_httpheader, Array (
               ' Content-type:application/json ',
               ' Content-length: '. strlen ($data _string)))
      ;

Note: application/json data that is encoded in the format $_POST is not directly available and needs to be file_get_contents(“php://input”) obtained by or $GLOBALS[‘HTTP_RAW_POST_DATA'] in a way.

Summarize

The above is about PHP Curl function send POST request Attention point, hope this article content for everyone's study or work can help, if there is doubt you can message exchange.

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.