Php 如何获取字段名';旋度响应中的s值

Php 如何获取字段名';旋度响应中的s值,php,string,parsing,curl,Php,String,Parsing,Curl,首先,我想说我是一个PHP初学者,我的代码绝对没有效率 我目前遇到的问题是,我想以一种方式解析CURL响应,以获取几个字段名值并将它们分配给一个变量。一旦我有了变量,我将向客户显示一些字段,向数据库显示一些字段。我知道在使用PayPal的API时,哪些字段将被发回,但是我不确定如何从响应中“抓取”它们 这是我目前正在使用的脚本,同样不是很有效,但是在CURL请求之后我被卡住了。非常感谢您的帮助 <?php include_once "constants.php"; foreach ($_

首先,我想说我是一个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];
}
打印($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
)

请向我们展示一个CURL响应的示例,并让我们知道您想要分配给vars的项目。下面是一个示例响应:
TIMESTAMP=2012-05-18T21:18:41Z&CORRELATIONID=c864cb25fc2d&ACK=Success&VERSION=87&BUILD=2929894&AM‌​T=1.00&CURRENCYCODE=USD&AVSCODE=X&CVV2MATCH=M&TRANSACTIONID=6AS92013633623055
我正在寻找交易ID、确认和时间戳。一些字段可能会根据我使用的服务而改变,但我认为如果我能看到格式,我可以从那里获取:)这里还有一个问题。如果将其分配给数组,则无法根据位置找到字段名/值。它必须基于字段名。这对我来说太棒了!当我尝试在测试中胡闹时,我错过了:
$newValues=explode('=',$value)$数据[$newValues[0]]=$newValues[1]我将在将来尝试创建更高效的代码,但我可以根据字段名完美地分配变量。非常感谢你,Ryan,因为这将使我进入下一个阶段:)