2012-03-01 191 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