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
216 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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
Number of reviews not counting correctly 2 22
mysql query for sum() 3 28
PHP Mail error 3 28
How to extract database info from current month and year 7 13
To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 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…

830 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