Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

file_get_content to simplexml_load_file PHP

Posted on 2013-11-05
4
Medium Priority
?
617 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 44

Accepted Solution

by:
Chris Stanyon earned 1200 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 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 111

Expert Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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

916 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