2015-09-23 69 views
3

我使用下面的腳本發佈的用戶數據,以支付網關:如何將參數從SimpleXMLElement對象分配給變量?

$map_url = "https://paynetzuat.atomtech.in/paynetz/epi/fts?login=160&[email protected]&ttype=NBFundTransfer&prodid=NSE&amt=50&txncurr=INR&txnscamt=0&clientcode=007&txnid=12345&date=19/09/2015&custacc=1234567890&udf1=vinodBisen&[email protected]&udf3=9890933093&udf4=mybillingaddress&ru=http://mysite.in/thankyou.html"; 

if (($response_xml_data = file_get_contents($map_url))===false) { 
    echo "Error fetching XML\n"; 
} else { 
    libxml_use_internal_errors(true); 
    $data = simplexml_load_string($response_xml_data); 

    if (!$data) { 
     echo "Error loading XML\n"; 
     foreach(libxml_get_errors() as $error) { 
      echo "\t", $error->message; 
     } 
    } else { 
     echo '<pre>'; 
     print_r($data); 
     echo '</pre>'; 
    } 
} 

並得到迴應如下:

SimpleXMLElement Object 
(
    [MERCHANT] => SimpleXMLElement Object 
     (
      [RESPONSE] => SimpleXMLElement Object 
       (
        [url] => https://mysite.api.url.com/fts 
        [param] => Array 
         (
          [0] => NBFundTransfer 
          [1] => 524014 
          [2] => K30ZsUL2Z5mD8NN1xS6FxIuV0YlOS2e1KPEKv0fT0Ms%3D 
          [3] => 1 
         )  
       )  
     )  
) 

我想每個響應到一個單獨的變量來創建使用由支付網關API接收的動態響應的鏈接。

回答

0

可以遍歷RESPONSE,然後在param影片投放到string提取名稱和值:

foreach($data->MERCHANT->RESPONSE as $response) { 

    $responseData = array(
     'url' => (string)$response->url 
    ); 

    foreach($response->param as $param) { 
     $responseData[(string)$param->attributes()->name] = (string)$param; 
    } 

    print_r($responseData); 
} 

輸出:

Array 
(
    [url] => https://paynetzuat.atomtech.in/paynetz/epi/fts 
    [ttype] => NBFundTransfer 
    [tempTxnId] => 524142 
    [token] => BL%2BmNz1kIv9%2FzTk8ei9iAxNmMGUJtt5jMip9Bfw1hG0%3D 
    [txnStage] => 1 
) 

然後,如果你想呼應token,你可以簡單地做到這一點:

echo $responseData['token']; 

如果只有一個響應,你可以跳過循環訪問RESPONSE

$responseData = array(
    'url' => (string)$data->MERCHANT->RESPONSE->url 
); 

foreach($data->MERCHANT->RESPONSE->param as $param) { 
    $responseData[(string)$param->attributes()->name] = (string)$param; 
} 

print_r($responseData); 
+0

謝謝:)它的工作! –