Access twitter profile by AJAX JQuery and XML

Posted By: Matpal Media - 10:53 AM
Jquery has a powerful library that can perform rich internet actions like AJAX and parsing the XML. We will use the same capability of Jquery for getting user profile of any twitter user.

Step 1 : Write a PHP file profile.php with following content

<?php
header ("Content-Type:text/xml");
$username="matpal";
$xml=file_get_contents("http://twitter.com/users/".$username);
echo $xml;
?> 
 

Step 2 : Write a simple profile.html to consume the XML


<?php
<head>
<script src="http://code.jquery.com/jquery-1.6.1.min.js" type="text/javascript">
</script>
<script type="text/javascript">
$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "profile.php",
    dataType: "xml",
    success: parseXml

  });
});

function parseXml(xml)
{
 
  $(xml).find("user").each(function()
  {
    
$("#output").append("<img src='"+$(this).find("profile_image_url").text()+"'/><br/>");
$("#output").append("("+$(this).find("screen_name").text() + ")<br />");
$("#output").append($(this).find("name").text() + "<br />");
$("#output").append($(this).find("description").text() + "<br />");
  });

 
}

</script>


</head>
<body>
<div id='output'>
</div>
</body>
?>

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.