2013-09-26 10 views
5

मुझे यह पता लगाने में कोई समस्या है कि ट्विटर पर हैशटैग का इस्तेमाल कितनी बार किया गया था। अतीत में मैंने निम्नलिखित कोड का उपयोग किया जो काम करता था लेकिन ट्विटर द्वारा "http://search.twitter.com/search.json" पता से सेवानिवृत्त हो गया है। पुराना कोड था:ट्विटर एपीआई 1.1 हैशटैग गणना

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

मुझे पता है कि आपको पता है कि एक अधिकृत ट्विटर एप्लिकेशन का उपयोग करना है और डेटा खींचने में सक्षम होना है। मैं ऐप सेट अप करने में सक्षम था और मैं निम्नलिखित कोड का उपयोग कर डेटा की एक सूची खींच सकता हूं लेकिन मुझे यकीन नहीं है कि कुल डेटा के साथ आने के लिए उस डेटा का उपयोग कैसे करें। क्या कोई मेरे पास या तो कोड बदलकर या मुझे इसके बारे में मदद करने के लिए मेरी सहायता करने में कुल मिलाकर मेरी सहायता कर सकता है। यहाँ कोड मेरे पास है हैशटैग डेटा खींचती है:

<?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); 
?> 

उत्तर

4

यहाँ के लिए ट्विटर 1.1 this से बनाया गया एपीआई एक उदाहरण है। ध्यान में रखें:

कृपया ध्यान दें कि ट्विटर की खोज सेवा और विस्तार से,, खोज API ट्वीट्स की एक विस्तृत स्रोत होने के लिए नहीं होती है। सभी ट्वीट्स को सूचीबद्ध किया जाएगा या खोज इंटरफ़ेस के माध्यम से उपलब्ध कराया जाएगा।

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

  1. डाउनलोड twitter-api-php और अपने सर्वर
  2. Create a Developer Account and an Application
  3. पर अपने फ़ोल्डर में निकालने के लिए एक फ़ाइल twitter_search.php (एक ही फ़ोल्डर में जहां चहचहाना-api-php है बनाएं)
  4. अपने टोकन और चाबियाँ twitter_search.php
  5. 01 में जोड़ें

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.

संबंधित मुद्दे