Recently, at work, I needed to request a Java version of an interface from a third party. At first, I used an array to pass parameters. After writing the code, I found that the interface could not be debugged. It always prompted that the passed parameters were empty. After struggling for a long time, I decided to try it. I used the method of passing string parameters to request the interface. Unexpectedly, the debugging was successful this time. This made me interested in studying the difference between the two.
The local test code is as follows:
curl.php
<code><span><span><?php </span><span><span>function</span><span>curl_post</span><span>(<span>$url</span>, <span>$data</span>)</span>{</span><span>$ch</span> = curl_init(); curl_setopt(<span>$ch</span>, CURLOPT_URL, <span>$url</span>); curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_POST, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, <span>$data</span>); <span>$output</span> = curl_exec(<span>$ch</span>); curl_close(<span>$ch</span>); <span>return</span><span>$output</span>; } <span>$params</span> = <span>array</span>(); <span>$params</span>[<span>'username'</span>] = <span>'ben'</span>; <span>$params</span>[<span>'password'</span>] = <span>'lalala'</span>; print_r(curl_post(<span>'http://localhost/curl/post.php'</span>, <span>$params</span>)); <span>$params</span> = <span>array</span>(); <span>$params</span>[<span>'username'</span>] = urlencode(<span>'ben'</span>); <span>$params</span>[<span>'password'</span>] = urlencode(<span>'lalala'</span>); <span>$paramsStr</span> = <span>"username={$params['username']}&password={$params['password']}"</span>; print_r(curl_post(<span>'http://localhost/curl/post.php'</span>, <span>$paramsStr</span>)); <span>?></span></span></span></code>
post.php
<code><span><?php </span><span>echo</span><span>"-------php://input-----<br>"</span>; var_dump(@file_get_contents(<span>'php://input'</span>)); <span>echo</span><span>"-------post-----<br>"</span>; var_dump(<span>$_POST</span>); <span>echo</span><span>"-------server-----<br>"</span>; var_dump(<span>$_SERVER</span>);</span></code>
The execution result can be referred to the screenshot below:
Copyright Statement: This article is an original article by the blogger. Please indicate the source and author name when reprinting. Respect others and respect yourself
The above introduces the difference between string and array value transfer in PHP using curl's post method, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.