2017-04-17 3 views
0

私の国では傾向のハッシュタグから人気のつぶやきを印刷しようとしています。まず、動向のハッシュタグを取得した後、動向のハッシュタグ内になるようにつぶやきURLのクエリを設定します。ツイートが1つのハッシュタグから取得されました - forloop ph

問題は、ツイートが1つのハッシュタグから取得され、それが最も傾向のあるハッシュタグであることです。また、何とかツイートが繰り返されます!

トレンドのハッシュタグから人気のつぶやきを繰り返し取り出すことはできますか?

これは私のコードです:

<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> 

:このコードでは、私はhtmlとTwitterAPIExchange.phpライブラリ

答えて

0

と一緒にコーディングするためにPHPを使用してい

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から最初のものを読み込みます - ループしないでください(またはループを1回だけ行います) –

+0

私はPHPを行っていないので、しばらくそれがうまくいけば近いです –

関連する問題