我首先想说我是 PHP 的初学者,至少可以说我的代码效率不高。
我目前遇到的问题是我想以某种方式解析 CURL 响应以获取多个字段名称值并将它们分配给一个变量。获得变量后,我将向客户显示一些字段,向数据库显示一些字段。我知道在使用 PayPal 的 API 时会发回哪些字段,但是我不确定如何从响应中“获取”它们。
这是我目前正在使用的脚本,同样不是很有效,但是在 CURL 请求之后我被卡住了。任何帮助是极大的赞赏!!
<?php
include_once "constants.php";
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$wppro = "$key=$value";
}
$ipaddress = "216.113.168.139";
$creditcardtype = $_POST["creditcardtype"];
$acct = $_POST["acct"];
$expmo = $_POST["expmo"];
$expyear = $_POST["expyear"];
$cvv = $_POST["cvv"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$street = $_POST["street"];
$street2 = $_POST["street2"];
$city = $_POST["city"];
$state = $_POST["state"];
$zip = $_POST["zip"];
$countrycode = $_POST["countrycode"];
$amt = $_POST["amt"];
$expdate = $expmo . $expyear;
$method = "DoDirectPayment";
$nvp = "version=$version&method=$method&user=$api_user&pwd=$api_pwd&signature=$api_sig&ipaddress=$ipaddress&$creditcardtype=$creditcardtype&acct=$acct&expdate=$expdate&cvv=$cvv&firstname=$firstname&lastname=$lastname&street=$street&street2=$street2&city=$city&state=$state&zip=$zip&countrycode=$countrycode&amt=$amt";
$ch = curl_init(); // Starts the curl handler
curl_setopt($ch, CURLOPT_URL,$sburl); // Sets the paypal address for curl
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // Returns result to a variable instead of echoing
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // Sets a time limit for curl in seconds (do not set too low)
curl_setopt($ch, CURLOPT_POST, 1); // Set curl to send data using post
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp); // Add the request parameters to the post
$httpResponse = curl_exec($ch); // run the curl process (and return the result to $result
curl_close($ch);
谢谢,
马特