Solved

How do I convert a HTTP GET Request in PHP into an ARRAY when the results return as an xml

Posted on 2014-11-13
7
218 Views
Last Modified: 2014-11-14
I have to query a url with http get and then utilise the results which return with XML.

https://cipg.stanbicibtcbank.com/MerchantServices/UpayTransactionStatus.ashx?MERCHANT_ID=06412&ORDER_ID=2014111209310244

How would I accomplish this please?
0
Comment
Question by:souldj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 40439573
Read the return file as an xml using any of the php library functions. For example simplexml_load_string.

http://php.net/manual/en/function.simplexml-load-string.php


Here is an article on reading and parsing a complex xml.

http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_11342-Reading-XML-Namespaces-using-PHP-Without-regex.html
0
 
LVL 1

Author Comment

by:souldj
ID: 40442170
so with the simplexml function,

would my syntax be




<?php
$url = "https://cipg.stanbicibtcbank.com/MerchantServices/UpayTransactionStatus.ashx?MERCHANT_ID=06412&ORDER_ID=2014111209310244";

$xml = simplexml_load_string($url);

print_r($xml);
?>
0
 
LVL 17

Assisted Solution

by:Chris Harte
Chris Harte earned 500 total points
ID: 40442603
No. you need to do a file_get_contents() to load the url into a string. This can then be read by simplexml

$url = "https://cipg.stanbicibtcbank.com/MerchantServices/UpayTransactionStatus.ashx?MERCHANT_ID=06412&ORDER_ID=2014111209310244";

$contents = file_get_contents($url);

$xml = simplexml_load_string($contents);


print_r($xml);

Open in new window


Or you could use the load_file format

$url = "https://cipg.stanbicibtcbank.com/MerchantServices/UpayTransactionStatus.ashx?MERCHANT_ID=06412&ORDER_ID=2014111209310244";

$xml = simplexml_load_file($url);


print_r($xml);

Open in new window


Both of these produces the output

SimpleXMLElement Object
(
    [MerchantID] => 06412
    [OrderID] => 2014111209310244
    [Status] => Successful
    [StatusCode] => 00
    [Amount] => 105.00
    [Date] => 12-11-2014 10:22
    [TransactionRef] => 2014111210220347T
    [PaymentRef] => GTB|WEB|IBTC|12-11-2014|006485
    [PaymentGateway] => MasterCard (Online Shopping Malls, Telemarketing, Online Pharmacy, Clubs, Bill Payment Sites)
    [ResponseCode] => 00
    [ResponseDescription] => Approved Successful
    [CurrencyCode] => 566
)

Open in new window

0
When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot has fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

 
LVL 1

Author Comment

by:souldj
ID: 40442761
This is exactly what I wanted help with, i have done it with ASP, but I am pretty new to PHP.

But the output comes as an array, if I want to work with any of the output like [StatusCode] how would i address the code  with an if statement

if statuscode = "00" then
 do this
else
 do that
end if statement
0
 
LVL 17

Accepted Solution

by:
Chris Harte earned 500 total points
ID: 40442780
$url = "https://cipg.stanbicibtcbank.com/MerchantServices/UpayTransactionStatus.ashx?MERCHANT_ID=06412&ORDER_ID=2014111209310244";

$xml = simplexml_load_file($url);

//Loop through the xml object to get each key-value pair
foreach ($xml as $key => $value)
{
    echo "$key => $value <br />";
}

//Or address the object property directly
echo "The order id is :". $xml->OrderID;


//Or use the value of the property
if ($xml->StatusCode == 00)
    echo "php is way better than asp";

Open in new window

0
 
LVL 1

Author Closing Comment

by:souldj
ID: 40442941
Munterman , you da Man! thanks a lot for these insights. Can I ask for a small favour, I want the values to be stored into a single variable

datalog = ""
foreach ($xml as $key => $value)
{
    datalog =  "$key => $value <br />";
}

something like this so I can log ALL the value.. Can you advise on this as well.
0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 40443207
You just have to cast it as an array

$datalog = (array)$xml;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to dynamically set the form action using jQuery.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question