Home >Backend Development >PHP Tutorial >The difference between using arrays and strings in CURLOPT_POSTFIELDS of PHP cURL

The difference between using arrays and strings in CURLOPT_POSTFIELDS of PHP cURL

WBOY
WBOYOriginal
2016-08-08 09:19:221806browse

We use the following two variables to do the cURL POST test:

<code><span><?php </span><span>// 数组</span><span>$arr_data</span> = [<span>'a'</span> => <span>'apple'</span>];
<span>// 字符串</span><span>$str_data</span> = <span>'{"a":"apple"}'</span>;</span></code>

Then the following is the result printed from the server side:
The difference between using arrays and strings in CURLOPT_POSTFIELDS of PHP cURL
The above figure analyzes the difference between CURLOPT_POSTFIELDS using arrays and strings from $_SERVER[‘CONTENT_TYPE’], $_POST variables, and php://input respectively.

Finally, we can conclude:
1) When using an array, the Content-Type of the HTTP request header will be set to multipart/form-data;

    The content of the array can be obtained from $_POST;
  • Use file_get_content("php://input") Is empty, this is also mentioned in the PHP manual:
  • php://input is not available with enctype="multipart/form-data"

    2) When using strings
The Content-Type of the HTTP request header will be set to application/x-www-form-urlencoded;
    The string content will become the key of an element in $_POST;
  • The string content can be completely retrieved from php Obtained from ://input;
Copyright statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger.

The above has introduced the difference between using arrays and strings in CURLOPT_POSTFIELDS of PHP cURL, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn