如何在 CURL 响应中获取字段名称的值


How to grab a field name's value in a CURL response

我首先想说我是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);

谢谢

马 特

这个怎么样? 它不是超级优雅,但我认为它会起作用。

$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
)