2013-09-26 150 views
5

Tôi gặp sự cố khi tìm cách trả về tổng số lần cho thẻ bắt đầu bằng # được sử dụng trên Twitter. Trong quá khứ, tôi đã sử dụng mã sau đây đã hoạt động nhưng địa chỉ "http://search.twitter.com/search.json" đã bị gỡ bỏ bởi Twitter. Mã cũ là:Twitter API 1.1 Số thẻ hashtag

<?php 
    global $total, $hashtag; 
    //$hashtag = '#supportvisitbogor2011'; 
    $hashtag = '#MyHashtag'; 
    $total = 0; 
    function getTweets($hash_tag, $page) { 
     global $total, $hashtag; 
     $url = 'http://search.twitter.com/search.json?q='.urlencode($hash_tag).'&'; 
     $url .= 'page='.$page;  
     $ch = curl_init($url); 
     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); 
     $json = curl_exec ($ch); 
     curl_close ($ch); 
     //echo "<pre>";  
     //$json_decode = json_decode($json); 
     //print_r($json_decode->results); 

     $json_decode = json_decode($json);   
     $total += count($json_decode->results);  
     if($json_decode->next_page){ 
     $temp = explode("&",$json_decode->next_page);   
     $p = explode("=",$temp[0]);     
     getTweets($hashtag,$p[1]); 
     }   
    } 

    getTweets($hashtag,1); 

    echo $total; 
?> 

Tôi biết rằng bạn biết phải sử dụng ứng dụng twitter được ủy quyền và có quyền truy cập để có thể lấy dữ liệu. Tôi đã có thể thiết lập ứng dụng và tôi có thể lấy danh sách dữ liệu bằng cách sử dụng mã sau nhưng tôi không chắc chắn cách sử dụng dữ liệu đó để tìm ra tổng số. Ai đó có thể giúp tôi để có được một tổng số bằng cách thay đổi mã tôi có hoặc giúp tôi với cách tôi nên đi về nó. Đây là mã tôi có để lấy dữ liệu hashtag:

<?php 
session_start(); 
require_once("twitteroauth.php"); //Path to twitteroauth library 

$hashtag = "MyHashtag"; 
$consumerkey = "MYINFOWOULDBEHERE"; 
$consumersecret = "MYINFOWOULDBEHERE"; 
$accesstoken = "MYINFOWOULDBEHERE"; 
$accesstokensecret = "MYINFOWOULDBEHERE"; 

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) { 
    $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret); 
    return $connection; 
} 

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); 

$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$hashtag); 

echo json_encode($tweets); 
?> 

Trả lời

4

Đây là một ví dụ cho API Twitter 1.1 được làm từ this. Lưu ý điều này:

Xin lưu ý rằng dịch vụ tìm kiếm của Twitter và, mở rộng, API tìm kiếm không có nghĩa là nguồn đầy đủ của Tweets. Không phải tất cả Tweets sẽ được lập chỉ mục hoặc có sẵn thông qua giao diện tìm kiếm.

https://dev.twitter.com/docs/api/1.1/get/search/tweets

  1. Tải twitter-api-php và giải nén nó vào thư mục của bạn tại máy chủ của bạn
  2. Create a Developer Account and an Application
  3. Tạo một file twitter_search.php (trong cùng một thư mục, nơi bạn có twitter-api-php)
  4. Thêm mã thông báo và khóa của bạn vào twitter_search.php

twitter_search.php:

<?php 
require_once('TwitterAPIExchange.php'); 

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/ 
$settings = array(
    'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN", 
    'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET", 
    'consumer_key' => "YOUR_CONSUMER_KEY", 
    'consumer_secret' => "YOUR_CONSUMER_SECRET" 
); 

/** Note: Set the GET field BEFORE calling buildOauth(); **/ 
$url = 'https://api.twitter.com/1.1/search/tweets.json'; 
$requestMethod = 'GET'; 
$hashtag = 'twitterapi'; 
$getfield = '?q=%23'.$hashtag.'&result_type=recent&include_entities=true&count=100'; 

// Perform the request 
$twitter = new TwitterAPIExchange($settings); 
$json_output = $twitter->setGetfield($getfield) 
      ->buildOauth($url, $requestMethod) 
      ->performRequest(); 

// Let's calculate how many results we got 
$json = json_decode($json_output); 
$n = count($json->statuses); 
echo $n; 
?> 

Using the Twitter Search API

I posted originally this example in here.