2017-04-17 91 views
0

我试图从热门标签在我的国家打印热门推文。首先,我获取了趋势标签,然后我在推文网址中设置查询,以便在趋势标签中。Tweets从一个hashtag检索 - forloop php

问题是,推文只从一个hashtag中获取,而且是最不热门的推文!此外,不知何故,鸣叫重复!

如何从热门标签无需重复获取热门推文?

这是我的代码:

<html> 
<?php 
use Abraham\TwitterOAuth\TwitterOAuth; 

require_once('twitter-api-php-master/TwitterAPIExchange.php'); 

$settings = array(
    'consumer_key' => '', 
    'consumer_secret' => '', 

    'oauth_access_token' => '', 
    'oauth_access_token_secret' => '',); 
$Saudi_id = 23424938; 
$requestMethod = 'GET'; 
$twitter = new TwitterAPIExchange($settings); 
/* the url that have the request*/ 
$url = 'https://api.twitter.com/1.1/trends/place.json'; 
/*appending values into the url*/ 
$get_request = '?id=' . $Saudi_id ; 
$json_data = $twitter->setGetfield($get_request)->buildOauth($url, $requestMethod)->performRequest(); 
/* converting JSON format into PHP object */ 
$redeable_data = json_decode($json_data); 
/* creating the TRENDS array linked with converted object*/ 
$trends = $redeable_data[0]->trends; 
// Set here the Twitter account from where getting latest tweets 
foreach($trends as $trend){ 
     # print only the hashtags the starts with # 
     if(preg_match("/^#/i",$trend->name)){ 
     /* prining the name of each hashtag along with the url for navigation as list*/ 
     $name=$trend->name; 
    } 
    } 
// Get timeline using TwitterAPIExchange 
$url = 'https://api.twitter.com/1.1/search/tweets.json'; 
$getfield = "q=".$name."&result_type=recent&count=700"; 
$tweets = $twitter 
    ->setGetfield($getfield) 
    ->buildOauth($url, $requestMethod) 
    ->performRequest(); 

$tweets = json_decode($tweets); 
echo "<ul>"; 
if(isset($tweets->statuses) && is_array($tweets->statuses)) { 
    if(count($tweets->statuses)) { 
     foreach($tweets->statuses as $tweet) { 
      echo "<li>"; 
    echo ($tweet->text); 
    echo "</li>"; 
    // if any image were included within the tweet , print it with size 20% 
    if (isset($tweet->entities->media)) { 
    $media_url = $tweet->entities->media[0]->media_url; 
    echo "<img src='{$media_url}' width='20%' />"; 
    } 
     } 
    } 
    else { 
     echo 'The result is empty'; 
    } 
} 
echo "</ul>"; 

?> 
</html> 

注意:我使用的PHP与HTML和TwitterAPIExchange.php库

回答

0

沿着代码在这个代码

foreach($trends as $trend){ 
     # print only the hashtags the starts with # 
     if(preg_match("/^#/i",$trend->name)){ 
     /* prining the name of each hashtag along with the url for navigation as list*/ 
     $name=$trend->name; 
    } 
    } 

你正在循环浏览大多数趋势标签到最少趋势,并且只保留最后一个为$名称 - 因此您最终只能获得最少的趋势。如果你想获得的鸣叫每个X的最流行的主题标签做这样的事情

$trend = reset($trends); 
$name = $trend->name; 

for ($x = 0; $x <= 5; $x++) 
     $trend = $trends[$x] 
     # print only the hashtags the starts with # 
     if(preg_match("/^#/i",$trend->name)){ 
     /* prining the name of each hashtag along with the url for navigation as list*/ 
     $name=$trend->name; 
     /*get the tweets here inside the loop*/ 
     $url = 'https://api.twitter.com/1.1/search/tweets.json'; 
     $getfield = "q=".$name."&result_type=recent&count=700"; 
     $tweets = $twitter 
     ->setGetfield($getfield) 
     ->buildOauth($url, $requestMethod) 
     ->performRequest(); 

    $tweets = json_decode($tweets); 
    echo "<ul>"; 
    if(isset($tweets->statuses) && is_array($tweets->statuses)) { 
     if(count($tweets->statuses)) { 
     foreach($tweets->statuses as $tweet) { 
     echo "<li>"; 
    echo ($tweet->text); 
    echo "</li>"; 
    // if any image were included within the tweet , print it with size 20% 
    if (isset($tweet->entities->media)) { 
     $media_url = $tweet->entities->media[0]->media_url; 
     echo "<img src='{$media_url}' width='20%' />"; 
    } 
    } 
    } 
    else { 
    echo 'The result is empty'; 
    } 
    } 
    echo "</ul>"; 
    } 
    } 
+0

你知道如何解决抢到只有第一个,这样做这个,请吗? –

+0

如果你只想要最流行的循环,那么只能从$ trends读取第一个循环 - 所以不要循环(或者只循环一次) –

+0

我添加了一些代码 - 未经测试,因为我还没有完成php一段时间,但它希望是接近 –