Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

how to decode posted parameters in php

Dear Experts,

I post a payment request to a payment website, then the web site returns me the below data as a succesful result.

I get the result like this

$raw = file_get_contents('php://input');
and when I print_r($raw); I get the below result.

I want to decode the result and put the parameters into arrays however I can not decode it properly.
How can I decode it?
I tried to decode it like below twice. When I did that, it only gives me the result but it doesn't give the header of the result.
what do you suggest I should do?
$new = urldecode($raw);
$new = urldecode($new);
DATA=%253c%253fxml%2Bversion%253d%25221.0%2522%2Bencoding%253d%2522utf-16%2522%253f%253e%253cHttpPostResponseMessage%2Bxmlns%253axsi%253d%2522http%253a%252f%252fwww.w3.org%252f2001%252fXMLSchema-instance%2522%2Bxmlns%253axsd%253d%2522http%253a%252f%252fwww.w3.org%252f2001%252fXMLSchema%2522%253e%253cResponse%253e%253cId%253e28%253c%252fId%253e%253cDateTime%253e2017-08-22%2B13%253a10%253a30Z%253c%252fDateTime%253e%253c%252fResponse%253e%253cOrder%253e%253cReference%253eHALDbRXQ1vNffcN7GKftmzfLyyTiEYFKezQyQPhHok%253c%252fReference%253e%253cDateTime%253e2017-08-22%2B16%253a08%253a53Z%253c%252fDateTime%253e%253cCustomReference%253ewebmaster%253c%252fCustomReference%253e%253c%252fOrder%253e%253cRequest%253e%253cId%253e39%253c%252fId%253e%253cStatusId%253e20%253c%252fStatusId%253e%253cError%2B%252f%253e%253c%252fRequest%253e%253cTransactions%253e%253cHPRMVPosTransaction%253e%253cId%253e193651%253c%252fId%253e%253cReference%253eKL40642P0000193651%253c%252fReference%253e%253cVPosId%253e1%253c%252fVPosId%253e%253cVPosApiId%253e80%253c%252fVPosApiId%253e%253cPaySetId%253e2%253c%252fPaySetId%253e%253cPaySetName%253eNzor%2BDepo%253c%252fPaySetName%253e%253cInstallment%253e1%253c%252fInstallment%253e%253cPlusInstallment%253e0%253c%252fPlusInstallment%253e%253cPaymentDeferral%253e0%253c%252fPaymentDeferral%253e%253cCommTypeId%253e10%253c%252fCommTypeId%253e%253cCommRate%253e0%252c0000%253c%252fCommRate%253e%253cIsTest%253efalse%253c%252fIsTest%253e%253cCardBrand%253e20%253c%252fCardBrand%253e%253cName%253e%25c3%2596zel%2B%25c3%2587%25c4%25b1moly%25c4%259flu%253c%252fName%253e%253cCCNumber%253e**********1111%253c%252fCCNumber%253e%253cDetail%253e%253cHPRMVPosTransactionDetail%253e%253cId%253e194001%253c%252fId%253e%253cStatus%253e1%253c%252fStatus%253e%253cType%253e40%253c%252fType%253e%253cDateTime%253e2017-08-22%2B16%253a09%253a19Z%253c%252fDateTime%253e%253cError%2B%252f%253e%253cAmount%253e1%252c00%253c%252fAmount%253e%253cNetAmount%253e1%252c00%253c%252fNetAmount%253e%253cProvisionNumber%253e097476%253c%252fProvisionNumber%253e%253c%252fHPRMVPosTransactionDetail%253e%253c%252fDetail%253e%253c%252fHPRMVPosTransaction%253e%253c%252fTransactions%253e%253c%252fHttpPostResponseMessage%253e&HASH=3644f5538552869aa14a00eeaf380b751b7d6b8d971a15ab66fcfcb6a646dcc0

Open in new window

Avatar of poohbear_68
poohbear_68
Flag of United States of America image

I believe you want something like this:

parse_str($raw, $output);
$raw_xml = urldecode($output['DATA']);
$xml = simplexml_load_string($raw_xml);

print_r($xml);

Open in new window

Avatar of BR

ASKER

Dear poohbear_68
thank you for your promt reply.

when I do that it gives me the result like this

DATA=282017-08-22 13:10:30ZHALDbRXQ1vNffcN7GKftmzfLyyTiEYFKezQyQPhHok2017-08-22 16:08:53Zwebmaster3920193651KL40642P00001936511802Nzor Depo100100,0000false20Özel Çımolyğlu**********32321940011402017-08-22 16:09:19Z1,001,00097476&HASH=3644f5538552869aa14a00eeaf380b751b7d6b8d971a15ab66fcfcb6a646dcc0

how can I separate them? it should give me like this : Reference => KL40642P0000193651 or Reference : KL40642P0000193651
we can use this function

$DATA="DATA=%253c%253fxml%2Bversio...";
$decDATA = urldecode (rawurldecode ($DATA));

Open in new window


result will be

<?xml version="1.0" encoding="utf-16"?><HttpPostResponseMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Response><Id>28</Id><DateTime>2017-08-22 13:10:30Z</DateTime></Response><Order><Reference>HALDbRXQ1vNffcN7GKftmzfLyyTiEYFKezQyQPhHok</Reference><DateTime>2017-08-22 16:08:53Z</DateTime><CustomReference>webmaster</CustomReference></Order><Request><Id>39</Id><StatusId>20</StatusId><Error /></Request><Transactions><HPRMVPosTransaction><Id>193651</Id><Reference>KL40642P0000193651</Reference><VPosId>1</VPosId><VPosApiId>80</VPosApiId><PaySetId>2</PaySetId><PaySetName>Nzor Depo</PaySetName><Installment>1</Installment><PlusInstallment>0</PlusInstallment><PaymentDeferral>0</PaymentDeferral><CommTypeId>10</CommTypeId><CommRate>0,0000</CommRate><IsTest>false</IsTest><CardBrand>20</CardBrand><Name>Özel Çımolyğlu</Name><CCNumber>**********1111</CCNumber><Detail><HPRMVPosTransactionDetail><Id>194001</Id><Status>1</Status><Type>40</Type><DateTime>2017-08-22 16:09:19Z</DateTime><Error /><Amount>1,00</Amount><NetAmount>1,00</NetAmount><ProvisionNumber>097476</ProvisionNumber></HPRMVPosTransactionDetail></Detail></HPRMVPosTransaction></Transactions></HttpPostResponseMessage>&HASH=3644f5538552869aa14a00eeaf380b751b7d6b8d971a15ab66fcfcb6a646dcc0

Open in new window

Avatar of BR

ASKER

dear hainkurt,

the result is the same as poohbear_68

DATA=282017-08-22 13:10:30ZHALDbRXQ1vNffcN7GKftmzfLyyTiEYFKezQyQPhHok2017-08-22 16:08:53Zwebmaster3920193651KL40642P00001936511802Nzor Depo100100,0000false20Özel Çımolyğlu**********32321940011402017-08-22 16:09:19Z1,001,00097476&HASH=3644f5538552869aa14a00eeaf380b751b7d6b8d971a15ab66fcfcb6a646dcc0

I use PHP version:  7.1
when you use your function, do you get a different result? is it because of my php version?
$DATA="DATA=%253c%253fxml%2Bversio...";
$decDATA = urldecode (rawurldecode ($DATA));
$xmlDATA = substr($decDATA, 0, strpos ($decDATA, "&HASH"));

Open in new window

I get

<?xml version="1.0" encoding="utf-16"?><HttpPostResponseMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Response><Id>28</Id><DateTime>2017-08-22 13:10:30Z</DateTime></Response><Order><Reference>HALDbRXQ1vNffcN7GKftmzfLyyTiEYFKezQyQPhHok</Reference><DateTime>2017-08-22 16:08:53Z</DateTime><CustomReference>webmaster</CustomReference></Order><Request><Id>39</Id><StatusId>20</StatusId><Error /></Request><Transactions><HPRMVPosTransaction><Id>193651</Id><Reference>KL40642P0000193651</Reference><VPosId>1</VPosId><VPosApiId>80</VPosApiId><PaySetId>2</PaySetId><PaySetName>Nzor Depo</PaySetName><Installment>1</Installment><PlusInstallment>0</PlusInstallment><PaymentDeferral>0</PaymentDeferral><CommTypeId>10</CommTypeId><CommRate>0,0000</CommRate><IsTest>false</IsTest><CardBrand>20</CardBrand><Name>Özel Çımolyğlu</Name><CCNumber>**********1111</CCNumber><Detail><HPRMVPosTransactionDetail><Id>194001</Id><Status>1</Status><Type>40</Type><DateTime>2017-08-22 16:09:19Z</DateTime><Error /><Amount>1,00</Amount><NetAmount>1,00</NetAmount><ProvisionNumber>097476</ProvisionNumber></HPRMVPosTransactionDetail></Detail></HPRMVPosTransaction></Transactions></HttpPostResponseMessage>

Open in new window

I got some error related to utf 8 / 16 but fixed as below

//decode and decode
$decDATA = urldecode (rawurldecode ($DATA));
//remove hash part
$xmlDATA = substr($decDATA, 0, strpos ($decDATA, "&HASH"));
//change encoding
$xmlDATA = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $xmlDATA);     
//load into xml object
$xml = simplexml_load_string($xmlDATA);

Open in new window


then we can use variables like this...

print_r($xml);

Open in new window


SimpleXMLElement Object
(
    [Response] => SimpleXMLElement Object
        (
            [Id] => 28
            [DateTime] => 2017-08-22 13:10:30Z
        )

    [Order] => SimpleXMLElement Object
        (
            [Reference] => HALDbRXQ1vNffcN7GKftmzfLyyTiEYFKezQyQPhHok
            [DateTime] => 2017-08-22 16:08:53Z
            [CustomReference] => webmaster
        )

    [Request] => SimpleXMLElement Object
        (
            [Id] => 39
            [StatusId] => 20
            [Error] => SimpleXMLElement Object
                (
                )

        )

    [Transactions] => SimpleXMLElement Object
        (
            [HPRMVPosTransaction] => SimpleXMLElement Object
                (
                    [Id] => 193651
                    [Reference] => KL40642P0000193651
                    [VPosId] => 1
                    [VPosApiId] => 80
                    [PaySetId] => 2
                    [PaySetName] => Nzor Depo
                    [Installment] => 1
                    [PlusInstallment] => 0
                    [PaymentDeferral] => 0
                    [CommTypeId] => 10
                    [CommRate] => 0,0000
                    [IsTest] => false
                    [CardBrand] => 20
                    [Name] => Özel Çımolyğlu
                    [CCNumber] => **********1111
                    [Detail] => SimpleXMLElement Object
                        (
                            [HPRMVPosTransactionDetail] => SimpleXMLElement Object
                                (
                                    [Id] => 194001
                                    [Status] => 1
                                    [Type] => 40
                                    [DateTime] => 2017-08-22 16:09:19Z
                                    [Error] => SimpleXMLElement Object
                                        (
                                        )

                                    [Amount] => 1,00
                                    [NetAmount] => 1,00
                                    [ProvisionNumber] => 097476
                                )

                        )

                )

        )

)
1

Open in new window

$xml->Response->Id

Open in new window


gives 28...
Avatar of BR

ASKER

Dear hainkurt, thank you very much. however it gives me below error:


Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/omercepost/public_html/js.php on line 15

Warning: simplexml_load_string(): DATA=<?xml version="1.0" encoding="utf-8"?><HttpPostResponseMessage xmlns:xsi="h in /home/omercepost/public_html/js.php on line 15

Warning: simplexml_load_string(): ^ in /home/omercepost/public_html/js.php on line 15

my code is:
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('log_errors', TRUE);
ini_set('error_log', 'error_log');

?>
<?php

$DATA="DATA=%253c%253fxml%2Bversion%253d%25221.0%2522%2Bencoding%253d%2522utf-16%2522%253f%253e%253cHttpPostResponseMessage%2Bxmlns%253axsi%253d%2522http%253a%252f%252fwww.w3.org%252f2001%252fXMLSchema-instance%2522%2Bxmlns%253axsd%253d%2522http%253a%252f%252fwww.w3.org%252f2001%252fXMLSchema%2522%253e%253cResponse%253e%253cId%253e28%253c%252fId%253e%253cDateTime%253e2017-08-22%2B13%253a10%253a30Z%253c%252fDateTime%253e%253c%252fResponse%253e%253cOrder%253e%253cReference%253eHALDbRXQ1vNffcN7GKftmzfLyyTiEYFKezQyQPhHok%253c%252fReference%253e%253cDateTime%253e2017-08-22%2B16%253a08%253a53Z%253c%252fDateTime%253e%253cCustomReference%253ewebmaster%253c%252fCustomReference%253e%253c%252fOrder%253e%253cRequest%253e%253cId%253e39%253c%252fId%253e%253cStatusId%253e20%253c%252fStatusId%253e%253cError%2B%252f%253e%253c%252fRequest%253e%253cTransactions%253e%253cHPRMVPosTransaction%253e%253cId%253e193651%253c%252fId%253e%253cReference%253eKL40642P0000193651%253c%252fReference%253e%253cVPosId%253e1%253c%252fVPosId%253e%253cVPosApiId%253e80%253c%252fVPosApiId%253e%253cPaySetId%253e2%253c%252fPaySetId%253e%253cPaySetName%253eNzor%2BDepo%253c%252fPaySetName%253e%253cInstallment%253e1%253c%252fInstallment%253e%253cPlusInstallment%253e0%253c%252fPlusInstallment%253e%253cPaymentDeferral%253e0%253c%252fPaymentDeferral%253e%253cCommTypeId%253e10%253c%252fCommTypeId%253e%253cCommRate%253e0%252c0000%253c%252fCommRate%253e%253cIsTest%253efalse%253c%252fIsTest%253e%253cCardBrand%253e20%253c%252fCardBrand%253e%253cName%253e%25c3%2596zel%2B%25c3%2587%25c4%25b1moly%25c4%259flu%253c%252fName%253e%253cCCNumber%253e**********1111%253c%252fCCNumber%253e%253cDetail%253e%253cHPRMVPosTransactionDetail%253e%253cId%253e194001%253c%252fId%253e%253cStatus%253e1%253c%252fStatus%253e%253cType%253e40%253c%252fType%253e%253cDateTime%253e2017-08-22%2B16%253a09%253a19Z%253c%252fDateTime%253e%253cError%2B%252f%253e%253cAmount%253e1%252c00%253c%252fAmount%253e%253cNetAmount%253e1%252c00%253c%252fNetAmount%253e%253cProvisionNumber%253e097476%253c%252fProvisionNumber%253e%253c%252fHPRMVPosTransactionDetail%253e%253c%252fDetail%253e%253c%252fHPRMVPosTransaction%253e%253c%252fTransactions%253e%253c%252fHttpPostResponseMessage%253e&HASH=3644f5538552869aa14a00eeaf380b751b7d6b8d971a15ab66fcfcb6a646dcc0";

$decDATA = urldecode (rawurldecode ($DATA));
$xmlDATA = substr($decDATA, 0, strpos ($decDATA, "&HASH"));
$xmlDATA = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $xmlDATA);     
$xml = simplexml_load_string($xmlDATA);

print_r($xml);


?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

you are great. thank you so much hainkurt,
actually, your nickname should be helpfulkurt, thank you so much...
Avatar of BR

ASKER

you are great. thank you hainkurt