2016-03-27 7 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私は次のようになります:---------注意:未定義の変数:/Applications/XAMPP/xamppfiles/htdocs/infoCraft-miner/search_server.php on 44行のTwitterSentinmentAnalysis –

+0

+1よく聞かれる質問特に、問題の原因となる完全なスクリプトを含め、すべての当事者にとって有用であることを証明したコメントに素早く対応しています。 – stratedge

答えて

0

変数ネーミングにタイプミスがあるように見えます(グローバルキーワードを削除すると通知が表示される)。

あなたが最初にあなたのオブジェクトを定義するとき、それは次のようになります。

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

だから$TwitterSentimentAnalysisです。しかし、後で$TwitterSentinmentAnalysisと参照しています。それは微妙ですが、第2の感情に余分なnがあります。

変数名を調整して、global行を削除してください(これは、グローバル変数が悪い形であると言うと、もっと上手な開発者が行くかもしれません)行くのが良い。

関連する問題