2016-03-27 31 views
1

我遇到了這個錯誤,我知道它期待一個對象,並且已經通過論壇進行搜索,嘗試應用修復時失敗,這可能是一個簡單的修復通過幾行代碼。我欣賞是否有人有一點時間來幫助解決這個問題。php調用成員函數sentimentAnalysis()非對象

同樣,這可能是重複的,但我曾嘗試應用修復未成功

錯誤與下面的線,我會包括我的整個腳本

$response= $TwitterSentinmentAnalysis->sentimentAnalysis($twitterSearchParams); 

Fatal error: Call to a member function sentimentAnalysis() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/infoCraft-miner/search_server.php on line 48

<?php 

// The search terms are passed in the q parameter 
// search_server.php?q=[search terms] 
if (!empty($_GET['q'])) { 

    // Remove any hack attempts from input data 
    $search_terms = htmlspecialchars($_GET['q']); 

    // Get the application OAuth tokens 
    require 'app_tokens.php'; 

    //get the datumbox api key 
    require 'config.php'; 
    require 'lib/TwitterSentimentAnalysis.php'; 

    $TwitterSentimentAnalysis = new TwitterSentimentAnalysis(DATUMBOX_API_KEY, 
    TWITTER_CONSUMER_KEY,TWITTER_CONSUMER_SECRET, 
    TWITTER_ACCESS_KEY,TWITTER_ACCESS_SECRET); 

    // Create an OAuth connection 
    require 'tmhOAuth.php'; 
    $connection = new tmhOAuth(array(
     'consumer_key' => $consumer_key, 
     'consumer_secret' => $consumer_secret, 
     'user_token'  => $user_token, 
     'user_secret'  => $user_secret 
    )); 

    // Request the most recent 100 matching tweets 
    $http_code = $connection->request('GET',$connection->url('1.1/search/tweets'), 
     $twitterSearchParams=array('q' => $search_terms, 
       'count' => 100, 
       'lang' => 'en', 
       'type' => 'recent')); 

    // Search was successful 
    if ($http_code == 200) { 

     // Extract the tweets from the API response 
     $response = json_decode($connection->response['response'],true);   
     global $TwitterSentinmentAnalysis; 

     //Response to be sent to Sentiment API 
     $response= $TwitterSentinmentAnalysis->sentimentAnalysis($twitterSearchParams);  

     $tweet_data = $response['statuses']; 

     //Sending the Twitter API response(JSONP) direct to a local file 
     $file = 'data.json'; 
     file_put_contents('data.json', json_encode($response)); 

     // Load the template for tweet display 
     $tweet_template= file_get_contents('tweet_template.html'); 

     // Load the library of tweet display functions 
     require 'display_lib.php'; 

     // Create a stream of formatted tweets as HTML 
     $tweet_stream = ''; 


     foreach($tweet_data as $tweet) { 

      //if loop to change text color  
      $color=NULL; 
      if($tweet['sentiment']=='positive'){    
       $color='#00FF00'; 
      } 
      else if($tweet['sentiment']=='negative'){ 
       $color='#FF0000'; 
      } 
      else if($tweet['sentiment']=='neutral'){ 
       $color='#FFFFFF'; 
      } 

      // Ignore any retweets 
      if (isset($tweet['retweeted_status'])) { 
       continue; 
      } 

      // Get a fresh copy of the tweet template 
      $tweet_html = $tweet_template; 

      // Insert this tweet into the html 
      $tweet_html = str_replace('[screen_name]', 
       $tweet['user']['screen_name'],$tweet_html); 
      $tweet_html = str_replace('[name]', 
       $tweet['user']['name'],$tweet_html);   
      $tweet_html = str_replace('[profile_image_url]', 
       $tweet['user']['profile_image_url'],$tweet_html); 
      $tweet_html = str_replace('[tweet_id]', 
       $tweet['id'],$tweet_html); 
      $tweet_html = str_replace('[tweet_text]', 
       linkify($tweet['text']),$tweet_html); 
      $tweet_html = str_replace('[created_at]', 
       twitter_time($tweet['created_at']),$tweet_html); 
      $tweet_html = str_replace('[retweet_count]', 
       $tweet['retweet_count'],$tweet_html);   

      // Add the HTML for this tweet to the stream 

      $tweet_stream .= $tweet_html; 

     } 

     // Pass the tweets HTML back to the Ajax request 
     print $tweet_stream; 

    // Handle errors from API request 
    } else { 
     if ($http_code == 429) { 
      print 'Error: Twitter API rate limit reached'; 
     } else { 
      print 'Error: Twitter was not able to process that search'; 
     } 
    } 

} else { 
    print 'No search terms found'; 
} 
?> 

這是它從TwitterSentimentAnalysis.php調用函數的文件

public function sentimentAnalysis($twitterSearchParams) { 
     $tweets=$this->getTweets($twitterSearchParams); 

     return $this->findSentiment($tweets); 
    } 

    /** 
+0

出於好奇,如果刪除'global $ TwitterSentinmentAnalysis;'行,會發生什麼? – stratedge

+0

@xjstratedgebx我收到以下內容---------注意:未定義的變量:TwitterSentinmentAnalysis中的/Applications/XAMPP/xamppfiles/htdocs/infoCraft-miner/search_server.php 44行 –

+0

對於一個問題很好的問題尤其是包括引起該問題的完整腳本,並快速回應最終對各方都有幫助的評論。 – stratedge

回答

0

在我看來,你的變量命名中有一個錯字(這就是爲什麼刪除全局關鍵字會拋出通知)。

當你第一次定義的對象,它看起來像這樣:

$TwitterSentimentAnalysis = new TwitterSentimentAnalysis(DATUMBOX_API_KEY, 
    TWITTER_CONSUMER_KEY,TWITTER_CONSUMER_SECRET, 
    TWITTER_ACCESS_KEY,TWITTER_ACCESS_SECRET); 

所以這是$TwitterSentimentAnalysis。但是,您之後將其引用爲$TwitterSentinmentAnalysis。這很微妙,但是第二個中有一個額外的n在情感上。

繼續前進並調整變量名稱,刪除global行,因爲它是不必要的,據我所知(而更多的開發人員可能甚至會說全局變量是不好的形式),並且我認爲,很好去。

相關問題