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庫
你知道如何解決搶到只有第一個,這樣做這個,請嗎? –
如果你只想要最流行的循環,那麼只能從$ trends讀取第一個循環 - 所以不要循環(或者只循環一次) –
我添加了一些代碼 - 未經測試,因爲我還沒有完成php一段時間,但它希望是接近 –