gpt4 book ai didi

PHP paypal ipn监听交易数组

转载 作者:太空宇宙 更新时间:2023-11-03 16:03:44 28 4
gpt4 key购买 nike

        $req = 'cmd=' . urlencode('_notify-validate');
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.paypal.com'));
$res = curl_exec($ch);
curl_close($ch);

if (strcmp ($res, "VERIFIED") == 0) {
//TODO
}
else if (strcmp ($res, "INVALID") == 0) {
//TODO
}

回应

cmd=_notify-validate&transaction=Array

响应还有更多内容,但您是否可以看到问题在于它们是 $_POST 数组中的数组。如何使用 $_POST 数组编码和去除数组的斜线?

最佳答案

数组中的数组在 PHP 中无法通过 $_POST 变量正确处理。

您需要直接从输入流中读取原始数据。

这里有一些示例代码。

<?php
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval)
{
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
$_req = 'cmd=_notify-validate';
foreach ($myPost as $key => $value)
{
$value = urlencode(stripslashes($value));
$_req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
//$header .= "Host: www.sandbox.paypal.com:443\r\n";
$header .= "Content-type: text/html; charset=utf-8\r\n";
$header .= "Content-Length: " . strlen($_req) . "\r\n\r\n";

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);


if (!$fp){
//Something failed
} else {
fputs ($fp, $header . $_req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {

//Success

}
else if (strcmp ($res, "INVALID") == 0) {
//Failure to validate
}
}
fclose ($fp);
}
?>

关于PHP paypal ipn监听交易数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11388139/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com