12 Replies - 872 Views - Last Post: 19 February 2012 - 03:38 PM Rate Topic: -----

#1 java_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-November 11

Displaying a Twitter Profile Picture from within XML file

Posted 18 February 2012 - 10:51 AM

Currently I'm displaying tweets from a geocode along with the tweet itself along with the user name and the time and date it was tweeted. But now Im trying to access the profile picture which within the XML. I have already accessed the link to the status which is contained within a link tag.

This is the tag I'm trying to access.
<link type="image/png" href="http://a0.twimg.com/profile_images/1706928570/IMG00474-20111008-1737_normal.jpg" rel="image" >


Here is the code that I currently have.

<?php


function get_twitter($feed) {

    $xml = new SimpleXmlElement(file_get_contents($feed));

    foreach ($xml->entry as $item) {
        $date = date("D, dS M Y g:i:sA T", strtotime($item->published));
       echo '<a href="' . $item->link->attributes()->href . '" target="_blank">' . $item->title . '</a><br />' . $item->author->name . '<br />' . '<strong>' . $date . '</strong>' . '<br />';
    
       
    }
}

?>


Any suggestions on how to access this and display it on the page?

Below is a screen show of what is being displayed from this PHP script.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Displaying a Twitter Profile Picture from within XML file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Displaying a Twitter Profile Picture from within XML file

Posted 18 February 2012 - 11:32 AM

Well let's see. You have link nodes that are actual links to documents, and you have link nodes that are links to images. Perhaps there might be some way to tell what kind of link node it is and display an img tag rather than an a tag?
Was This Post Helpful? 0
  • +
  • -

#3 java_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-November 11

Re: Displaying a Twitter Profile Picture from within XML file

Posted 18 February 2012 - 11:44 AM

Well I know how to show it as an image but I'm unsure on how to tell which link node it is.

I thought about it of using link[1] but that doesn't work so heres the code that I have currently come up with.

<img src='.$item->link->attributes()->href .'/>'

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Displaying a Twitter Profile Picture from within XML file

Posted 18 February 2012 - 11:52 AM

Can you provide an example of the XML? Not everyone is familiar with what you're doing.
Was This Post Helpful? 0
  • +
  • -

#5 java_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-November 11

Re: Displaying a Twitter Profile Picture from within XML file

Posted 18 February 2012 - 11:58 AM

The URL I'm using is
http://search.twitter.com/search.atom?geocode=51.5069999695%2C-0.142489999533%2C10.0mi%22london%22&lang=en&rpp=5


Here is an example of the XML
<feed xmlns:google="http://base.google.com/ns/1.0" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.w3.org/2005/Atom" xmlns:twitter="http://api.twitter.com/" xmlns:georss="http://www.georss.org/georss" xml:lang="en-US" >
       <id>tag:search.twitter.com,2005:search/</id>
       <link type="text/html" href="http://search.twitter.com/search?q=" rel="alternate" >
       <link type="application/atom+xml" href="http://search.twitter.com/search.atom?q=" rel="self" >
       <title>- Twitter Search</title>
       <link type="application/opensearchdescription+xml" href="http://twitter.com/opensearch.xml" rel="search" >
       <link type="application/atom+xml" href="http://search.twitter.com/search.atom?since_id=170944502142476289&q=&geocode=51.5069999695%2C-0.142489999533%2C10.0mi%22london%22" rel="refresh" >
       <updated>2012-02-18T18:55:19Z</updated>
       <openSearch:itemsPerPage>15</openSearch:itemsPerPage>
       <link type="application/atom+xml" href="http://search.twitter.com/search.atom?page=2&max_id=170944502142476289&q=&geocode=51.5069999695%2C-0.142489999533%2C10.0mi%22london%22" rel="next" >
       <entry>
              <id>tag:search.twitter.com,2005:170944502142476289</id>
              <published>2012-02-18T18:55:19Z</published>
              <link type="text/html" href="http://twitter.com/LondITjobs/statuses/170944502142476289" rel="alternate" >
              <title>Software Development Engineer II - London #job #jobs #hiring #career http://t.co/VsxPdWdT</title>
              <content type="html" >Software Development Engineer II - London #job #jobs #hiring #career http://t.co/VsxPdWdT</content>
              <updated>2012-02-18T18:55:19Z</updated>
              <link type="image/png" href="http://a3.twimg.com/profile_images/1812202768/jr_100_normal.png" rel="image" >
              <twitter:geo>
              <google:location>London</google:location>
              <twitter:metadata>
                     <twitter:result_type>recent</twitter:result_type>
                 </twitter:metadata>
              <twitter:source>twitterfeed</twitter:source>
              <twitter:lang>en</twitter:lang>
              <author>
                     <name>LondITjobs (ITjobsLondon)</name>
                     <uri>http://twitter.com/LondITjobs</uri>
                 </author>
          </entry>


The image I'm trying to access is within line 18.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Displaying a Twitter Profile Picture from within XML file

Posted 18 February 2012 - 12:01 PM

Think logically. Obviously you can have more than one link per entry. Perhaps another loop?
Was This Post Helpful? 0
  • +
  • -

#7 java_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-November 11

Re: Displaying a Twitter Profile Picture from within XML file

Posted 18 February 2012 - 12:24 PM

Something along these lines?

<?php

function get_twitter($feed) {

    $xml = new SimpleXmlElement(file_get_contents($feed));

    foreach ($xml->entry as $item) {
        $date = date("D, dS M Y g:i:sA T", strtotime($item->published));
       echo '<a href="' . $item->link->attributes()->href . '" target="_blank">' . $item->title . '</a><br />' . $item->author->name . '<br />' . '<strong>' . $date . '</strong>' . '<br />';
           
    }
    foreach ($xml->entry->link as $item){
        echo '<img src='.$item->link->attributes()->href .'/>'.'<br />';
        
                
             
    }
}

?>

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Displaying a Twitter Profile Picture from within XML file

Posted 18 February 2012 - 03:16 PM

Aren't the multiple links part of an entry?
Was This Post Helpful? 0
  • +
  • -

#9 java_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-November 11

Re: Displaying a Twitter Profile Picture from within XML file

Posted 19 February 2012 - 03:45 AM

Yes both links are within the entry therefore it will not be necessary to have link->link.

I have now changed my code to this

<?php function get_twitter($feed) {

    $xml = new SimpleXmlElement(file_get_contents($feed));

    
    foreach ($xml->entry as $item) {
       
        $date = date("D, dS M Y g:i:sA T", strtotime($item->published));
     
        echo '<a href="' . $item->link->attributes()->href . '" target="_blank">' . $item->title . '</a><br />' .'<img src='.$item->link->link->attributes()->href .'/>'.'<br />'. $item->author->name . '<br />' . '<strong>' . $date . '</strong>' . '<br />';
    }

}

?>


But it is showing this.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Displaying a Twitter Profile Picture from within XML file

Posted 19 February 2012 - 05:05 AM

Hmm...that XML looks wrong. I think those link tags aren't properly closed? Do you have a link to the source of the data?
Was This Post Helpful? 0
  • +
  • -

#11 java_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-November 11

Re: Displaying a Twitter Profile Picture from within XML file

Posted 19 February 2012 - 05:19 AM

View Postjava_newbie, on 18 February 2012 - 11:58 AM, said:

The URL I'm using is
http://search.twitter.com/search.atom?geocode=51.5069999695%2C-0.142489999533%2C10.0mi%22london%22&lang=en&rpp=5

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Displaying a Twitter Profile Picture from within XML file

Posted 19 February 2012 - 05:35 AM

OK, final hint:

<?php

function get_twitter($feed) {
   $xml = simplexml_load_string(file_get_contents($feed));

   foreach ($xml->entry as $item) {
      echo "Found " . count($item->link) . " links in this entry\n";
      foreach($item->link as $link) {
         echo "Found link of type {$link->attributes()->type}\n";
      }
   }
}

get_twitter("http://search.twitter.com/search.atom?geocode=51.5069999695%2C-0.142489999533%2C10.0mi%22london%22&lang=en&rpp=5");


?>



$ php -f twitter.php 
Found 2 links in this entry
Found link of type text/html
Found link of type image/png
Found 2 links in this entry
Found link of type text/html
Found link of type image/png
Found 2 links in this entry
Found link of type text/html
Found link of type image/png
Found 2 links in this entry
Found link of type text/html
Found link of type image/png
Found 2 links in this entry
Found link of type text/html
Found link of type image/png

Was This Post Helpful? 0
  • +
  • -

#13 java_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-November 11

Re: Displaying a Twitter Profile Picture from within XML file

Posted 19 February 2012 - 03:38 PM

Have finally worked out the solution.

Heres my code I have now.

<?php

function get_twitter($feed) {

    $xml = new SimpleXmlElement(file_get_contents($feed));

        foreach ($xml->entry as $item) {
       
        $date = date("D, dS M Y g:i:sA T", strtotime($item->published));
   
        echo '<a href="' . $item->link->attributes()->href . '" target="_blank">' . $item->title . '</a><br />' . '<a href="' . $item->author->uri . '">' . '<img src=http://api.twitter.com/1/users/profile_image/' . $item->author->name .'width="55" height="55"'. '>' . '</a>' . '<br />' . $item->author->name . '<br />' . '<strong>' . $date . '</strong>' . '<br />';
    }
}

?>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1