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.
Related
I need to place my variable $theCompany into the end of a URI for a VoiceURI on Twilio. As you can see below I've managed to create the variable but I can't figure how to put it into the URI. When we submit the page the VoiceURI field in Twilio is www.ourdomain.com/.xml.
The same is also true for the xml file I'm trying to create which saves as $theCompany.xml
The code is below, help appreciated!
Here's the line I'm using to populate the VoiceURI in Twilio:
'VoiceUrl' => "http://www.ourdomain.com/$theCompany.xml",
And here's the line I'm using to save the xml file with a new name/ same name as passed from previous into Twilio
$doc->save('"$theCompany".xml');
It's probably really simple but this isn't my normal game, I'm more on the Infusionsoft side of things but the code and web guy is on Honeymoon!
Appreciate the assistance!
--EDIT--
Thanks for the answers so far, unfortunately they don't seem to be working. Here's the full code for the PHP xml creator:
<?php
session_start();
?>
<?php
$theCompany = $_SESSION['company'];
$doc = new DOMDocument( );
$ele = $doc->createElement( 'Root' );
$ele->nodeValue = 'This is a call for $_SESSION["company"] press any key to accept the call';
$doc->appendChild( $ele );
$doc->save("$theCompany.xml");
?>
I also need some help getting the $theCompany into the URL on the Buy Number PHP page seen below.
<?php
session_start();
?>
<?php
// this line loads the library
require('Services/Twilio.php');
$theCompany = $_SESSION['company'];
$account_sid = 'AC7841a99c892xxxbc8f7xxx';
$auth_token = 'a71cxx052080xx';
$client = new Services_Twilio($account_sid, $auth_token);
$phoneNumber = $client->account->incoming_phone_numbers->create(array(
'PhoneNumber' => $_SESSION["number"],
'VoiceUrl' => "http://www.ourdomain.com/"'$theCompany .'".xml",
));
echo $phoneNumber->sid;
?>
The pages follow like so:
1) We have a PHP page to find available numbers. This page then passes the information onto the PHP page (code directly above this one)
2) That page buys the number and adds it to the account along with the VoiceURI and once submitted the page passes to the XML creator page
I have a feeling I should switch the buy and xml pages around so we search for a number then create the XML file then buy the number but not sure if that matters?
Thanks for sticking with me!
--EDIT #2--
Hi guys, sorry about this I know you're all helping best you can. I'm still having trouble with this so I'm thinking it might be best to create the XML file and pass that as a variable to the PHP file that sends info into Twilio. If we were to create the XML with $doc->save($theCompany.'.xml'); how would we pass that as a variable to the next page in place of $doc->save($theCompany.'.xml');?
I think it makes more sense to create the variables then to add them in place of the URI which is attempting to be a hybrid of static and dynamic.
So I'd be looking at something like this:
$phoneNumber = $client->account->incoming_phone_numbers->create(array(
'PhoneNumber' => $_SESSION["number"],
'VoiceUrl' => $theXML,
));
Do you think that's a better option to the route I'm taking now?
Use this
$doc->save($theCompany.".xml");
If I haven't misunderstood, this seems like a really simple problem.
Either you can write variables in a string using double quotes, like this:
$doc->save("$theCompany.xml");
Or you use single quotes for clarity, making it easier to look at:
$doc->save($theCompany . '.xml');
On your first code bit you wrote this:
$ele->nodeValue = 'This is a call for $_SESSION["company"] press any key to accept the call';
You have to make sure the session variable is escaped, like this:
$ele->nodeValue = 'This is a call for ' . $_SESSION["company"] . ' press any key to accept the call';
And for your second code bit, your phoneNumber variable has to be properly escaped:
$phoneNumber = $client->account->incoming_phone_numbers->create(array(
'PhoneNumber' => $_SESSION["number"],
'VoiceUrl' => 'http://www.ourdomain.com/' . $theCompany . '.xml',
));
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 )).
i want to create profile, i tried this way but it didn't worked..
forum.php
<?php
session_start();
if(!file_exists('users/' . $username . '.xml')){
header('Location: index.php');
die;
}
?>
header.php
<?php
$xmlFile = new DOMDocument();
$xmlFile->load('users/'. $username .'.xml');
echo $xmlFile->getElementsByTagName('name')->item(0)->nodeValue;
?>
user.xml
<?xml version="1.0"?>
<user>
<name>yotam</name>
<lastname>dahan</lastname>
<password>73e872fc06e6de8562d1d040268dafc9</password>
<birthday>13.7.1999</birthday>
<country>Israel</country>
<gender>Male</gender>
<age>13</age>
<email>yotking789#walla.com</email>
<level>Admin</level>
</user>
I know the problem in $xmlFile->load('users/'. $username .'.xml'); here but i can't make specific file because i got diffrent members every time and i cannot create each one page.
Any other way to get $username define in xml file load?
Your code works as it should but what is the purpose here? If you are looking to store user details you should use a database such as MySQL which is then used to lookup this data instead of XML.
If you are getting errors then have you got PHP set to display errors? This might give you some idea what is 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);
?>