?
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
Medium Priority
?
222 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 2000 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
2017 Webroot Threat Report

MSPs: Get the facts you need to protect your clients.
The 2017 Webroot Threat Report provides a uniquely insightful global view into the analysis and discoveries made by the Webroot® Threat Intelligence Platform to provide insights on key trends and risks as seen by our users.

 
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 2000 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

Threat Trends for MSPs to Watch

See the findings.
Despite its humble beginnings, phishing has come a long way since those first crudely constructed emails. Today, phishing sites can appear and disappear in the length of a coffee break, and it takes more than a little know-how to keep your clients secure.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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 viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

764 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