我首先要说的是我是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);
谢谢,
马特
答案 0 :(得分:0)
这个怎么样?它不是超级优雅,但我认为它会起作用。
$response = 'TIMESTAMP=2012-05-18T21:18:41Z&CORRELATIONID=c864cbb25fc2d&ACK=Success&VERSION=87&BUILD=2929894&AMT=1.00&CURRENCYCODE=USD&AVSCODE=X&CVV2MATCH=M&TRANSACTIONID=6AS92013633623055';
$response = explode('&',$response);
$data = array();
foreach($response AS $key => $value){
$newValues = explode('=',$value);
$data[$newValues[0]] = $newValues[1];
}
print_r($data)
会产生......
Array
(
[TIMESTAMP] => 2012-05-18T21:18:41Z
[CORRELATIONID] => c864cbb25fc2d
[ACK] => Success
[VERSION] => 87
[BUILD] => 2929894
[AMT] => 1.00
[CURRENCYCODE] => USD
[AVSCODE] => X
[CVV2MATCH] => M
[TRANSACTIONID] => 6AS92013633623055
)