php 在非对象上调用成员函数 sentimentAnalysis()

php Call to a member function sentimentAnalysis() on a non-object

我遇到了这个错误,我知道它需要一个对象,并且已经在论坛中进行了搜索,但在尝试应用修复时失败了,这可能是通过几行代码轻松解决的问题。如果有人有时间帮助解决此问题,我将不胜感激。

同样,这可能是重复的,但我已尝试应用修复,但未成功

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

 $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);
    }

    /**

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

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

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

那就是 $TwitterSentimentAnalysis。但是您稍后将其引用为 $TwitterSentinmentAnalysis。很微妙,但是第二个的Sentiment里多了一个n

继续调整变量名,删除 global 行,据我所知这是不必要的(而且更傲慢的开发人员可能会说全局变量是错误的形式),我想你可以走了。