我正在构建一个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);
}
子字符串比较是否有效? – 2011-06-13 14:19:55
该程序的每个元素都可以完美地工作,它只在我将它们放在一起时才会出现故障,子字符串比较不起作用,因为$ images()数组没有奇怪地累积任何链接 – 2011-06-13 14:45:18