I am attempting to use SimpleXML to parse the xml file that is produced from the Google Reader export.
File example:
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>TheTechBox subscriptions in Google Reader</title>
</head>
<body>
<outline text="Engadget RSS Feed" title="Engadget RSS Feed"
type="rss" xmlUrl="http://www.engadget.com/rss.xml" htmlUrl="http://www.engadget.com"/>
<outline text="xkcd.com" title="xkcd.com" type="rss"
xmlUrl="http://xkcd.com/rss.xml" htmlUrl="http://xkcd.com/"/>
</body>
</opml>
This is what I have tried so far, the user uploads the file to this form and the form needs to loop through and extract the data.
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$import = new SimpleXMLElement($_FILES["file"]["name"]);
foreach($import->opml->body->outline[0] as $feed){
echo $feed["title"];
}
}
?>
Right now chrome produces a server error which indicates something is very wrong on the page, the file uploads OK so it appears to be the simpleXML part.
I am planning to do something more complex with the data later, I am currently trying to get it just to echo the data on the page (for demo purposes).
Any feedback would be greatly appreciated.
Here is the working code in the end
$import = simplexml_load_file($_FILES["file"]["tmp_name"]);
foreach($import->body->outline as $feed){
echo $feed["title"];
}
This will need adding to to parse all of the data but this works.
You've fallen into a classic trap when using SimpleXML: the first object you get when parsing a file or string is not an abstract "document" object, but the parent node. In this case, the parent node is <opml> ... </opml>, so $import in your sample code is that <opml> node. In other words, rather than $import->opml->body you just need to say $import->body.
There's another bug in your loop as well, which is that you are asking for the first <outline> element (->outline[0]) when what you want is to be looping over all of the elements (foreach( $whatever->outline as $feed )).
Related
I have a URL that returns an XML formatted list
i have put this URL in a variable:
$url = 'urlHere.xml';
i need to be able to return the results and list them in PHP (for example in a select element)
i have tried using the following:
$xml = simplexml_load_file($url);
but im not too sure what would be next
the start of the XML file looks like:
<interface-response>
<tldlist>
<tld>
<tld>com</tld>
</tld>
<tld>
<tld>net</tld>
</tld>
Assume this is our xml file stored in the same dir
sms.xml
<?xml ?>
<sms>
<body>
Hi Doe! whats going on?
</body>
</sms>
Now lets get the contant of sms.xml in php
<?php
$xml = simplexml_load_file("sms.xml");
echo $xml->body;
?>
Output :
Hi Doe! whats going on?
I'm new in php. I recently go through one login example in php (http://net.tutsplus.com/articles/news/build-a-login-and-registration-system-with-xml/) and implemented login page for my test site in local host.
But now i want to assing each user one image let say i have 3 user and i have 3 user xml under user folder and each xml should look something like this
<?xml version="1.0"?>
<user>
<password>some password will come here</password>
<email>user#user.com</email>
<img>images/mynewimage.jpg</img>
</user>
i added <img> node in xml but i don't know how to call that on php page. Here the biggest challenge is for me is to find out the current user on each page since i have 3-4 basic pages after login and i'm maintain his login on each page so i want his image also on all the page i given below code on each page to maintain session
<?php
session_start();
if(!file_exists('users/' . $_SESSION['username'] . '.xml')){
header('Location: login.php');
die;
}
?>
when i check with username <?php echo ucfirst($_SESSION['username']); ?> it shows me current user name but in same way i'm unable to call image next to username.
use simplexml_load_file()
$xml = simplexml_load_file('users/' . $_SESSION['username'] . '.xml');
echo $xml->img;
the first thing you have to do is parse your xml file.php have a function for this:
$xmls = simple_xml_load_file(your_file's_exact_path);
$image = $xml->img;
if you have many img elements in your xml file you have to use foreach loop for that like:
foreach($xmls as $xml)
{
$xml->img;
$image = implode(',',$$xml->img;);
}
this code parse you xml file and save all images to the variable $image. now you can use it as you want.make sure about the img element's path. just try with this. it will help you.
happy coding!
I am attempting to send html data in a question form from my php web application to mechanical turk so a user can see the entire html document from an email to work with. I have had difficulty thus far. In the thread linked below, I attempted to parse the html data using html5-lib.php, but I think I'm still missing a step in order to complete this.
Here's the current error I'm receiving:
Catchable fatal error: Object of class DOMNodeList could not be converted to string in urlgoeshere.php on line 35
Here's the current code I'm working with...
$thequestion = 'click here';
$thequestion = HTML5_Parser::parseFragment($thequestion);
var_dump($thequestion);
echo $thequestion;
//htmlspecialchars($thequestion);
$QuestionXML = '<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">
<Question>
<QuestionContent>
<Text>'.$thequestion.'</Text> //<--- Line35
</QuestionContent>
<AnswerSpecification>
<FreeTextAnswer/>
</AnswerSpecification>
</Question>
</QuestionForm> ';
I'm not 100% sure if the parser is what I need to do in order to have this sent correctly - All I want to do is send html through this xml type document - I'm surprised it's been this difficult thus far.
This is somewhat of a continuation of another thread -
What PHP code will help me parse html data in an xml form?
Take a look at DOMDocument for working with DOM/xml in PHP. If you want to embed HTML in your XML, use CDATA sections like this:
$QuestionXML = '<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">
<Question>
<QuestionContent>
<Text><![CDATA['.$thequestion.']]></Text>
</QuestionContent>
<AnswerSpecification>
<FreeTextAnswer/>
</AnswerSpecification>
</Question>
</QuestionForm> ';
Not sure exactly what you're after. This is how i would create the XML needed to be transferred.
Please let me know if i misunderstood the question
It also seems that you are missing the QuestionIdentifier node which is mandatory according to the .xsd file.
<?
$dom = new DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$QuestionForm = $dom->createElement('QuestionForm');
$QuestionForm->setAttribute('xmlns','http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd');
// could loop this part for all the questions of the XML
$thequestion = 'click here';
//Not sure what this is supposed to be, but its required. Check the specs of the app for it.
$questionID = "";
$Question = $dom->createElement('Question');
$QuestionIdentifier = $dom->createElement('QuestionIdentifier',$questionID);
$QuestionContent = $dom->createElement('QuestionContent');
$QuestionContent->appendChild($dom->createElement('Text',$thequestion));
$AnswerSpecification = $dom->createElement('AnswerSpecification');
$AnswerSpecification->appendChild($dom->createElement('FreeTextAnswer'));
$Question->appendChild($QuestionIdentifier);
$Question->appendChild($QuestionContent);
$Question->appendChild($AnswerSpecification);
$QuestionForm->appendChild($Question);
// End loop
$dom->appendChild($QuestionForm);
$xmlString = $dom->saveXML();
print($xmlString);
?>
I have a problem when using simplexml to read a xml document that I am getting back from a webservice call.
Reading the data is fine however one node called UserArea contains a nested XMLdocument which contains namespaces.
From this question on SO I've looked at how to deal with child nodes. However when I call the node that has this nested XML in it I get null back.
The data looks like this:
<UserArea>
<rm:EngineVersion>4.2.0.62</rm:EngineVersion>
<rm:DocumentFormat>305</rm:DocumentFormat>
<rm:Industry>AUT</rm:Industry>
<rm:Department>GEN</rm:Department>
<rm:HighestDegree year="2004" major="COMPUTER PROGRAMMING">BACHELORS</rm:HighestDegree>
<rm:ExperienceSummary>
<rm:Experience>
<rm:ExperienceKind>Summary</rm:ExperienceKind>
<rm:Years>11</rm:Years>
<rm:Detail>A total of 11 years of work experience.</rm:Detail>
</rm:Experience>
<rm:Experience>
<rm:ExperienceKind>HighestIndustry</rm:ExperienceKind>
<rm:Years>5</rm:Years>
<rm:Industry>AUT</rm:Industry>
<rm:Detail>Highest industry-related experience is 5 years in automotive </rm:Detail>
</rm:Experience>
</rm:ExperienceSummary>
</UserArea>
I am out of ideas because the code:
foreach($myObject->UserArea->children as $userAreaXML){
foreach($userAreaXML->ExperianceSummary as $summary){
echo $summary->Detail;
}
}
just doesn't work.
You might want to read http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/ .. Couldn't be explained much clearer.
This code will print out the details
$experiences = $myObject->ExperienceSummary->Experience;
foreach($experiences as $experience) {
echo $experience->Detail . "<br>";
}
Im using simplexml to get the twitter profile avatar url from the xml status page.
this is the code im using
<?
$username = twitter;
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
echo $xml->user->profile_image_url;
?>
The xml page loads when i visit it, but for some reason nothing is being echoed. No errors. Nothing.
When i visit it in a browser, i get this:
<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>783214</id>
<name>Twitter</name>
<screen_name>twitter</screen_name>
<location>San Francisco, CA</location>
<description>Always wondering what everyone's doing.</description>
<profile_image_url>http://a1.twimg.com/profile_images/75075164/twitter_bird_profile_normal.png</profile_image_url>
<url>http://twitter.com</url>.....
(the rest is long and irrelevant to the question)
The data is there, why wont it echo?
After loading the XML document, the root element user is represented by the SimpleXMLElement object saved in $xml. Therefore $xml->user does not exist.
This one should work:
<?
$username = "twitter"; // <-- You did not use quotes here?! Typo?
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
echo $xml->profile_image_url; // <-- No $xml->user here!
?>
It is because the root element (in this case, <user>) is implied - you do not have to specify it.
Try this:
echo $xml->profile_image_url;
hey guys - tried out twivatar but I was getting an error 500 error????
anyway here is the code I ended up using - how it is helpful
<?php
if (isset($_REQUEST['user']))
//if "user" is filled out, get the user's twitter picture
{
$username = $_REQUEST['user'];
}else{
$username = 'aplusk';
//else lets use ashton kutcher's picture just for the F of it
} // <-- You did not use quotes here?! Typo?
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
$timage = $xml->profile_image_url; // <-- No $xml->user here!
header('location: ' . $timage);
?>
Now it is extremely easy to call this script from anywhere in your application simply print ...
<img src="http://mywebapp.org/timage.php?user=<php echo $data['twitter_screen_name'];?>" />
Twivatar
Previously, the easiest way to get the twitter avatar was to use Twivatar, which was built by Remy Sharp. However the service has since been shut down.
Using a third party for this is unnecessary.