php工作有奇怪的行为。我通过POST传递json数据并期望数据出现在php://input
中。而不是它使用奇怪的键/值对填充$ _POST。这是curl call
$process = curl_init("https://www.myurl.com/script");
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($process, CURLOPT_SSL_VERIFYHOST, FALSE);
$params = '{"Response":{"Data":{"RMID":"0910403545","QID":"965102499460","RspCode":"000","RspDesc":"Successful Transaction Complete","TrxID":"61801","TrxStatus":"COMPLETE","BID":"61801","TrxRC":"4201","TrxTime":"2015-06-15 14:53:51","TrxValue":"9"}},"Signature":"5bf094adb23e40e1de135c055684dd2098ab18d0","Certificate":"-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
5bf094adb23e40e1de135c055684dd2098ab18d0
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
iD8DBQFVfoSPQ8sozXgiBRURArdkAKCwx5ggf5dE+djAAMIDsHaapLRgOACePyys
a5scG9GhRFDefGz5CLHrqfI=
=AWpR
-----END PGP SIGNATURE-----
"}';
curl_setopt($process, CURLOPT_POST, true);
curl_setopt($process, CURLOPT_POSTFIELDS, $params);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($process);
另一方面,我得到了
echo "<pre>";
var_export($_POST);
---------------------------------------------
array (
'{"Response":{"Data":{"RMID":"0910403545","QID":"965102499460","RspCode":"000","RspDesc":"Successful_Transaction_Complete","TrxID":"61801","TrxStatus":"COMPLETE","BID":"61801","TrxRC":"4201","TrxTime":"2015-06-15_14:53:51","TrxValue":"9"}},"Signature":"5bf094adb23e40e1de135c055684dd2098ab18d0","Certificate":"-----BEGIN_PGP_SIGNED_MESSAGE-----
Hash:_SHA1
5bf094adb23e40e1de135c055684dd2098ab18d0
-----BEGIN_PGP_SIGNATURE-----
Version:_GnuPG_v1_4_5_(GNU/Linux)
iD8DBQFVfoSPQ8sozXgiBRURArdkAKCwx5ggf5dE_djAAMIDsHaapLRgOACePyys
a5scG9GhRFDefGz5CLHrqfI' => '
=AWpR
-----END PGP SIGNATURE-----
"}',
)
为什么这样做?
答案 0 :(得分:1)
关于curl_setopt($process, CURLOPT_POSTFIELDS, $params);
此参数既可以作为urlencoded字符串传递,例如'para1 = val1&amp; para2 = val2&amp; ...',也可以作为数组传递。 (ref)
cURL期待一个参数/值对,并且您只提供了一个字符串,因此cURL假定参数是第一个=
符号和值是之后的所有内容,直到遇到第一个&
符号,没有任何符号。这就是为什么$_POST
在转储它时会这样看的原因。
缓解此问题的一种方法是对$params
字符串进行urlencode并将其传递到curl_setopt
"data=".rawurlencode($params)
,然后使用rawurldecode($_POST["data"]);
检索它。