2012-03-01 189 views
3

我通過CURL使用post中的數據發送一串數據,並接收字符串中的響應以及數組中的表單。我試圖將這種響應轉換爲實際的數組。繼承人的代碼,我到目前爲止有:將字符串(與數組格式)轉換爲數組 - PHP

響應字符串即時得到是這樣的:

測試TRANSACTION_ID = 「2401_02-29-12_22:28:34_0」 行動= 「支付」 結果=「成功「to_from =」to「amount =」205.00「gross =」205.00「 net =」 - 0.85「custom =」「business =」[email protected]「item_name =」XXXXX.XXX「 item_code =」Product Name 3 「quantity =」1「transaction_type =」authorized payment「transaction_status =」pending「transaction_date =」2012-02-29 22:28:34「processing_rate =」0.85「discount_fee =」0.00「 first_name =」TESTNAME「 last_name =「TESTLNAME」address =「 TESTADDRESS「 city =」TESTCITY「state_or_province =」AL「country =」US「 zip_or_postal_code =」12345「phone =」1234562002「email =」[email protected]「 shipment =」yes「shipping_address =」TESTADDRESS「shipping_city = 「TESTCITY」 shipping_state_or_province = 「AL」 shipping_country = 「US」 shipping_zip_or_postal_code = 「12345」 PROCESSING_TIME = 「0.2187」

而且使用得到這樣的響應代碼IM是

<?php 

// Get variables from POST array into a string 
$post_str = "action=payment&business="  .urlencode($this->input->post('business')) 
    ."&vericode="     .urlencode($this->input->post('vericode')) 
    ."&item_name="     .urlencode($this->input->post('item_name')) 
    ."&item_code="     .urlencode($this->input->post('item_code')) 
    ."&quantity="     .urlencode($this->input->post('quantity')) 
    ."&amount="      .urlencode($this->input->post('amount')) 
    ."&cc_type="     .urlencode($this->input->post('cc_type')) 
    ."&cc_number="     .urlencode($this->input->post('cc_number')) 
    ."&cc_expdate="     .urlencode($this->input->post('cc_expdate_year')).urlencode($this->input->post('cc_expdate_month')) 
    ."&cc_security_code="   .urlencode($this->input->post('cc_security_code')) 
    ."&shipment="     .urlencode($this->input->post('shipment')) 
    ."&first_name="     .urlencode($this->input->post('first_name')) 
    ."&last_name="     .urlencode($this->input->post('last_name')) 
    ."&address="     .urlencode($this->input->post('address')) 
    ."&city="      .urlencode($this->input->post('city')) 
    ."&state_or_province="   .urlencode($this->input->post('state_or_province')) 
    ."&zip_or_postal_code="   .urlencode($this->input->post('zip_or_postal_code')) 
    ."&country="     .urlencode($this->input->post('country')) 
    ."&shipping_address="   .urlencode($this->input->post('shipping_address')) 
    ."&shipping_city="    .urlencode($this->input->post('shipping_city')) 
    ."&shipping_state_or_province=" .urlencode($this->input->post('shipping_state_or_province')) 
    ."&shipping_zip_or_postal_code=".urlencode($this->input->post('shipping_zip_or_postal_code')) 
    ."&shipping_country="   .urlencode($this->input->post('shipping_country')) 
    ."&phone="      .urlencode($this->input->post('phone')) 
    ."&email="      .urlencode($this->input->post('email')) 
    ."&ip_address="     .urlencode($this->input->post('ip_address')) 
    ."&website_unique_id="   .urlencode($this->input->post('website_unique_id')); 

// Send URL string via CURL 
$backendUrl = "https://www.veripayment.com/integration/index.php"; 
$this->curl->create($backendUrl); 
$this->curl->post($post_str); 

// get response from API 
$return_str = $this->curl->execute(); 
?> 

回答

1

如果您收到的回覆中有元素用空格分隔,您可能使用

function parse($response) 
{ 
    $result = array(); 
    $resparray = explode(' ', $response); 
    if ($resparray) 
    { 
     foreach ($resparray as $resp) { 
     $keyvalue = explode('=', $resp); 
     $result[$keyvalue[0]] = str_replace('"', '', $keyvalue[1]); 
     } 
    } 
    return $result; 
} 

編輯和糾正,下面是輸出

Array ([transaction_id] => 2401_02-29-12_22:28:34_0 [action] => payment [result] => success [to_from] => to [amount] => 205.00 [gross] => 205.00 [net] => -0.85 [custom] => [business] => [email protected] [item_name] => XXXXX.XXX [item_code] => Product [Name] => [3,"] => [quantity] => 1 [transaction_type] => authorized [payment"] => [transaction_status] => pending [transaction_date] => 2012-02-29 [22:28:34"] => [processing_rate] => 0.85 [discount_fee] => 0.00 [first_name] => TESTNAME [last_name] => TESTLNAME [address] => TESTADDRESS [city] => TESTCITY [state_or_province] => AL [country] => US [zip_or_postal_code] => 12345 [phone] => 1234562002 [email] => [email protected] [shipment] => yes [shipping_address] => TESTADDRESS [shipping_city] => TESTCITY [shipping_state_or_province] => AL [shipping_country] => US [shipping_zip_or_postal_code] => 12345 [processing_time] => 0.2187 
+0

卡洛斯我得到了一些指標的一些錯誤。這似乎是其skiping一些值: 一個PHP錯誤遇到 嚴重性:注意 消息:未定義抵消:1 文件名:管理/ test.php的 行號:65 線路:$ result [$ keyvalue [0]] = str_replace(''','',$ keyvalue [1]); – 2012-03-01 18:54:32

+0

如果任何傳遞的字符串中有空格,則此方法失敗。部分'transaction_type =「授權付款」'。您的代碼將其轉換爲'[transaction_type] =>授權[payment]] =>'。 它看起來像使這種方法的工作將要求您使用正則表達式或東西,所以你可以拆分字符串只在不在引號內的空格。我覺得必須有更好的方法... – octern 2012-03-01 18:56:41

+1

我在foreach中添加了一條if語句,所以它跳過了錯誤,現在我得到數組([transaction_id] => 2364_03-01-12_14:29:40_0 [action] => payment [result] => failed [errors] => 98 [errors_meaning] =>(98)[customer_errors_meaning] => [processing_time] => 0.0372) 這是我需要的主鍵。謝謝 – 2012-03-01 20:25:45