Solved

file_get_content to simplexml_load_file PHP

Posted on 2013-11-05
4
548 Views
Last Modified: 2013-11-05
Hello Experts,

I am currently working on a PHP project that implements calls to Web Services as part of its feature.

When a call is made to a URI, it is made using file_get_content. The result is an XML string. However, since I am more comfortable working with simplexml_load_file, I would like to convert the result that I get from the file_get_content call to a form that can be used by simplexml_load_file.

Something like this:

$xml = file_get_contents($request, false, $context);
//Change $xml to a format that can be used by simplexml_load_file
$ChangedXML = simplexml_load_file($xml);

FYI:

- When I try to make the call directly to the URI using simplexml_load_file, it throws an error. Besides, I would rather use what the API has recommended me to use, and they have recommended file_get_content to get the XML details when making calls.

- Also, when I try to directly load the result into simplexml_load_file, it throws an error.

It looks like some sort of formatting needs to be done to convert the results obtained from file_get_content to something that simplexml_load_file can use to load.


I am relatively new to PHP and would seek some assistance to this effect. I thank you all in advance.

Regards,

ref-IT
0
Comment
Question by:ref-IT
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 300 total points
ID: 39624310
Instead of using  simplexml_load_file() just use simplexml_load_string()

$xml = file_get_contents($request, false, $context);
$ChangedXML = simplexml_load_string($xml);

Open in new window

You then manipulate it the same way
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39624389
New to PHP?  We have some good learning resources for you here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

The library is documented in the online man pages.
http://php.net/manual/en/book.simplexml.php
0
 
LVL 2

Author Closing Comment

by:ref-IT
ID: 39624472
Hey Chaps,

I'll have to tell you this, you guys are spot on !!

Ray_Paseur, I truly appreciate all the links and I'll definitely go through them.

Thanks a lot chaps,

red-IT
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39624495
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

914 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