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
210 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 16

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 16

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 16

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 16

Expert Comment

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

$datalog = (array)$xml;
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Creating a slider 12 34
session dropped in IE 10 22
XML & .net 5 21
two tables one button 11 20
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now