从一个主题标签中检索推文 - forloop php

时间:2017-04-17 13:42:51

标签: php api for-loop twitter foreach

我正在尝试在我的国家/地区打印热门主题标签中的热门推文。首先,我获取了趋势主题标签然后我将推文URL中的查询设置为趋势主题标签。

问题是推文只从一个标签中获取,而且它是最不受欢迎的一个!此外,不知何故,推文重演了!

如何在不重复的情况下从趋势标签中获取流行的推文?

这是我的代码:

<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库进行编码

1 个答案:

答案 0 :(得分: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;
    }
    }

您正在遍历热门主题标签,从最热门到最不热门,只保留最后一个为$ name - 所以最终趋势最差。要只获取第一个,请执行以下操作:

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

如果您想获取每个X最受欢迎的主题标签的推文,请执行以下操作:

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>";
    }
    }