2012-05-18 116 views
1

我首先想說我是一名PHP初學者,我的代碼絕對沒有效率。如何在CURL響應中獲取字段名稱的值

我目前遇到的問題是我想解析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

請告訴我們捲曲響應的樣本,讓我們知道您要分配到瓦爾的項目。 – Ryan

+0

下面是一個示例響應: 'TIMESTAMP = 2012-05-18T21:18:41Z&的correlationID = c864cbb25fc2d&ACK =成功&VERSION = 87&BUILD = 2929894&AM T = 1.00&CURRENCYCODE = USD&AVSCODE = X&CVV2MATCH = M&TRANSACTIONID = 6AS92013633623055' 我想找搶交易ID,ACK和時間戳。有些字段可能會根據我正在使用的服務而改變,但我認爲如果我能看到格式,我可以從那裏檢索它:)這裏有另一個問題。如果我將它分配給數組,我無法根據位置找到字段名稱/值。它必須基於字段名稱。 – Matt

回答

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 
) 
+0

這對我來說很棒!當我在測試過程中嘗試搞亂時,我錯過了: '$ newValues = explode('=',$ value); $ data [$ newValues [0]] = $ newValues [1];' 我將嘗試在將來創建更高效​​的代碼,但我可以根據字段名稱完美地分配變量。謝謝sooo多瑞安,因爲這將讓我到一個新的水平:) – Matt

相關問題