0

I'm building a PHP program that basically grabs only image links from my twitter feed and displays them on a page, I have 3 components that I have set up that all work fine on their own.

The first component is the twitter oauth component which grabs the tweet text and creates an array, this works fine by itself.

The second is a function that processes the tweets and only returns tweets that contain image links, this as well works fine.

The program breaks down during the third section when the links are processed and an image is displayed, I had no issues running this on its own and from my attempts to trouble shoot it appears that it breaks down at the $images(); array, as that array is empty.

I'm sure I've made a silly mistake but I've been trying to find this for over a day now and can't seem to fix it. Any help would be great! Thanks guys!

code:

<?php

if ($result['socialorigin']== "twitter"){
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($result['oauthtoken'], $result['oauthsecret']);
$tweets = $twitterObj->get('/statuses/home_timeline.json',array('count'=>'200'));

         $all_tweets = array();
         $hosts  = "lockerz|yfrog|twitpic|tumblr|mypict|ow.ly|instagr";



         foreach($tweets as $tweet) {

         $twtext = $tweet->text;

         if(preg_match("~http://($hosts)~", $twtext)){

         preg_match_all("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", $twtext, $matches, PREG_PATTERN_ORDER);

         foreach($matches[0] as $key2 => $link){

         array_push($all_tweets,"$link");

         }

         }

         }
function height_compare($a1, $b1)
{
    if ($a1 == $b1) {
        return 0;
    }
    return ($a1 > $b1) ? -1 : 1;
}

foreach($all_tweets as $alltweet => $tlink){
$doc = new DOMDocument();
// Okay this is HTML is kind of screwy
// So we're going to supress errors
@$doc->loadHTMLFile($tlink);

// Get all images
$images_list = $doc->getElementsByTagName('img');

$images = array();
foreach($images_list as $image) {

  // Get the src attribute
  $image_source = $image->getAttribute('src');

  if (substr($image_source,0,7)=="http://"){
  $image_size_info = getimagesize($image_source);

  $images[$image_source] = $image_size_info[1];
  }
}



// Do a numeric sort on the height
uasort($images, "height_compare");
$tallest_image = array_slice($images, 0,1);
$mainimg = key($tallest_image);

echo "<img src='$mainimg' />";


 }
 print_r($all_tweets);
 print_r($images);

}
2
  • Do the substring comparison ever work? Commented Jun 13, 2011 at 14:19
  • each element of the the program works perfectly on their own, it only breaks down when I put them all together, the substring comparison isn't working because for some reason the $images() array isnt accumulating any links strangely Commented Jun 13, 2011 at 14:45

1 Answer 1

1

Change the for loop where you fetch the actual images to move the images array OUTSIDE the for loop. This will prevent the loop from clearing it each time through.

  $images = array();
foreach($all_tweets as $alltweet => $tlink){
  $doc = new DOMDocument();
  // Okay this is HTML is kind of screwy
  // So we're going to supress errors
  @$doc->loadHTMLFile($tlink);

  // Get all images
  $images_list = $doc->getElementsByTagName('img');    
  foreach($images_list as $image) {

    // Get the src attribute
    $image_source = $image->getAttribute('src');

    if (substr($image_source,0,7)=="http://"){
      $image_size_info = getimagesize($image_source);

      $images[$image_source] = $image_size_info[1];
    }
  }

  // Do a numeric sort on the height
  uasort($images, "height_compare");
  $tallest_image = array_slice($images, 0,1);
  $mainimg = key($tallest_image);

  echo "<img src='$mainimg' />";
}
Sign up to request clarification or add additional context in comments.

1 Comment

unfortunately I moved the array above the foreach loop and still no luck, when I print the array it still returns empty, I dont understand why this is happening, if I run that part of the code with just an array or links it works perfectly, but in conjunction with the other parts it all goes down the tube

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.