2016-03-27 32 views
1

Bu hatayı düşünerek bir sorunum var, bir nesne beklediğini biliyorum ve forumlarda arama yaptım ve düzeltmeleri uygulamaya çalışırken başarısız oldum, bu kolay bir düzeltme olabilir Birkaç satır kod ile. Bunu düzeltmek için biraz zamanınız varsa, teşekkür ederiz.php Nesne olmayan bir öğe için sentimentAnalysis() işlevini çağırın

Yine bu kopyası olabilir ama un-başarıyla

Hata aşağıdaki satırla olan düzeltmeleri uygulamak için çalıştık, benim tüm senaryoyu içerecektir

$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'; 
} 
?> 

Dosya, bu işlevi çağıran sözcüğü TwitterSentimentAnalysis.php

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

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

    /** 
+0

Merak etme, 'global $ TwitterSentinmentAnalysis' satırını kaldırırsanız ne olur? – stratedge

+0

@xjstratedgebx Aşağıdakileri alıyorum --------- Not: Tanımsız değişken: TwitterSentinmentAnalysis in /Applications/XAMPP/xamppfiles/htdocs/infoCraft-miner/search_server.php 44 –

+0

+1 iyi sorulan soru için özellikle de soruna neden olan tam metin dahil olmak üzere ve tüm taraflara faydalı olduğunu kanıtlayan bir yoruma yanıt vermek için hızlı davranmak. – stratedge

cevap

0

Değişken adında bir yazım hatası var gibi görünüyor (bu yüzden global anahtar kelimenin kaldırılması bir uyarı veriyor). öncelikle nesneyi tanımlarken

, bu şuna benzer:

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

Yani $TwitterSentimentAnalysis bu. Ancak daha sonra $TwitterSentinmentAnalysis olarak başvuruyorsunuz. İğrenç, ama ikincisinde Sentiment'te fazladan bir tane var.

Devam edin ve değişken adını ayarlayın, global satırını söyleyebildiğim kadar gereksiz hale getirin (ve global değişkenler kötüyse, daha kötü bir şey olabilir). gitmek güzel.

İlgili konular