【问题标题】:php Call to a member function sentimentAnalysis() on a non-objectphp调用非对象上的成员函数sentimentAnalysis()
【发布时间】:2016-03-27 01:08:23
【问题描述】:

我遇到了这个错误,我知道它需要一个对象,并在论坛中搜索并尝试应用修复时失败,这可能是几行代码的简单修复。我很感激是否有人有时间来帮助解决这个问题。

同样,这可能是重复的,但我尝试应用修复失败

错误在于以下行,我将包括我的整个脚本

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

致命错误:调用成员函数sentimentAnalysis() 非对象在 /Applications/XAMPP/xamppfiles/htdocs/infoCraft-miner/search_server.php 第 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);
    }

    /**

【问题讨论】:

  • 出于好奇,如果删除global $TwitterSentinmentAnalysis; 行会怎样?
  • @xjstratedgebx 我得到以下信息 ---------注意:未定义的变量:第 44 行 /Applications/XAMPP/xamppfiles/htdocs/infoCraft-miner/search_server.php 中的 TwitterSentinmentAnalysis
  • +1 提出了一个很好的问题,特别是包括导致问题的完整脚本,并快速回复最终证明对各方都有帮助的评论。
  • @xjstratedgebx 感谢您的帮助!!

标签: php json twitter fatal-error sentiment-analysis


【解决方案1】:

在我看来,您的变量命名中有错字(这就是为什么删除 global 关键字会引发通知)。

当你第一次定义你的对象时,它看起来像这样:

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

那就是$TwitterSentimentAnalysis。但是,您稍后将其引用为$TwitterSentinmentAnalysis。这很微妙,但在第二个中的 Sentiment 中有一个额外的 n。

继续调整变量名,删除 global 行,因为据我所知这是不必要的(而且更自负的开发人员可能会说全局变量是不好的形式),我认为你'很好走。

【讨论】:

    猜你喜欢
    • 2014-06-19
    • 2016-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多