2011-06-13 108 views
0

我正在構建一個PHP程序,基本上只抓取來自我的Twitter提要的圖像鏈接並將它們顯示在頁面上,我有3個組件,我已經設置好了所有工作都很好。傳遞數組值時出現問題

第一個組件是twitter oauth組件,它抓取tweet文本並創建一個數組,它本身很好地工作。

第二個是處理推文並只返回包含圖像鏈接的推文的函數,這也很好。

程序在第三節處理鏈接並顯示圖像時發生故障,我沒有任何問題可以自行運行,並且從嘗試排除故障的嘗試看起來,它在$ images( );數組,因爲該數組是空的。

我敢肯定,我犯了一個愚蠢的錯誤,但我一直在試圖找到這一點,現在似乎無法修復它。任何幫助將是偉大的!多謝你們!

代碼:

<?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); 

} 
+0

子字符串比較是否有效? – 2011-06-13 14:19:55

+0

該程序的每個元素都可以完美地工作,它只在我將它們放在一起時纔會出現故障,子字符串比較不起作用,因爲$ images()數組沒有奇怪地累積任何鏈接 – 2011-06-13 14:45:18

回答

1

更改for循環,你獲取的實際圖像到images陣列移動以外的for循環。這將防止循環每次都清除它。

$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

不幸的是我移動了foreach循環上面的數組,仍然沒有運氣,當我打印數組它仍然返回空,我不明白爲什麼會發生這種情況,如果我運行的代碼只有一個數組或鏈接它完美的作品,但聯合與其他部分一起下降管 – 2011-06-13 14:42:42