The difference between string and array value passing in PHP using curl's post method

WBOY
Release: 2016-07-29 09:14:04
Original
1247 people have browsed it

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>
Copy after login

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>
Copy after login

The execution result can be referred to the screenshot below:
The difference between string and array value passing in PHP using curls post method

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template