开发者

having problems passing array values

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, 开发者_JAVA百科$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);

}


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' />";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜