Solved

file_get_content to simplexml_load_file PHP

Posted on 2013-11-05
4
544 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 42

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 look for a specific file type in a local or remote server directory using PHP.

760 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

21 Experts available now in Live!

Get 1:1 Help Now