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
215 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Insert won't work due to space in column name 5 22
Help needed with Powershell  XML to MySQL 5 46
Fulfillment API php code sample 1 41
How would I do this...? 2 28
New Relic recently released its Synthetics product that allows for the creation of performance monitors that periodically test a site's performance. If you wish to test an interactive workflow New Relic employs Selenium WebDriverJS to run those test…
The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

803 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