Solved

file_get_content to simplexml_load_file PHP

Posted on 2013-11-05
4
561 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
[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
  • 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 110

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 110

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Security 29 71
Help with mod_substitute 18 35
$_GET call between URL 3 38
Echo'd values in dropdowns 6 28
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
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 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 …

735 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