2015-09-10 56 views
1

我在Amazon AWS上使用我的nginx webserver發生此問題。Jquery在nginx上調用php,不起作用

首先,這段代碼正在另一臺服務器上工作和測試,但我需要遷移到我自己的服務器,這裏是我遇到問題的地方。 我有以下的jQuery + PHP代碼或HTML頁面,它不是正在執行這樣的事情:

<div class="widget widget-twitter-feed clearfix"> 
<h4>Twitter Feed</h4> 
    <ul id="sidebar-twitter-list-1" class="iconlist"> 
     <li></li> 
    </ul> 

    <a href="https://twitter.com/coinnova_ok" class="btn btn-default btn-sm fright">Follow Us on Twitter</a> 

    <script type="text/javascript"> 
     jQuery(function($){ 
     $.getJSON('include/twitter/tweets.php?username=coinnova_ok', function(tweets){ 
     $("#sidebar-twitter-list-1").html(sm_format_twitter(tweets)); 
     }); 
     }); 
    </script> 

這是我的虛擬主機配置上的nginx:

server { 
    listen   80; 
    server_name localhost; 

    location ~ \.php$ { 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
    } 

    location ~ \.html$ { 
     root   html; 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.htm; 
     include  fastcgi.conf; 
    } 

    location ~ \.js$ { 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include  fastcgi_params; 
    } 
} 

我也更改這些我/etc/php-fpm.d/www.conf設置

security.limit_extensions = .php .html .js 

我希望有人能幫助我:)

更多信息

這是需要執行的tweets.php腳本。我想補充它只是供參考,但它在另一臺服務器上,以及:

<?php 
session_start(); 

if(isset($_GET['username']) AND $_GET['username'] != ''): 

    require_once('oauth/twitteroauth.php'); //Path to twitteroauth library 

    $username = $_GET['username']; 
    $limit = (isset($_GET['count']) AND $_GET['count'] != '') ? $_GET['count'] : 2; 
    $consumerkey = "LAxZjcBtFOw1wYhJ2Tqm8w"; 
    $consumersecret = "0SyeOk44o70ya2EVweHgJtwqBkr6DaoO0CETToFKsI"; 
    $accesstoken = "329620819-0tbQsQ7yx9BV5E6SzPaP39UAlDBuUokwRqgw075H"; 
    $accesstokensecret = "iL0divFR8xjTWQj9de511YxBpKkduE8dcOCRRlMrM"; 

    function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) { 
     $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret); 
     return $connection; 
    } 

    $interval = 600; 

    $cache_file = dirname(__FILE__) . '/cache/' . $username . '_' . $limit; 

     if (file_exists($cache_file)) { 
       $last = filemtime($cache_file); 
     } else { $last = false; } 

     $now = time(); 

     if (!$last || (($now - $last) > $interval)) { 

       $context = stream_context_create(array(
         'http' => array(
           'timeout' => 3 
         ) 
       )); 

     $connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); 
     $twitter_feed = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$username."&count=".$limit); 

       $cache_rss = serialize($twitter_feed); 

       if (!empty($cache_rss)) { 
         $cache_static = fopen($cache_file, 'wb'); 
         fwrite($cache_static, $cache_rss); 
         fclose($cache_static); 
       } 

       $rss = @unserialize(file_get_contents($cache_file)); 
     } else { 
     $rss = @unserialize(file_get_contents($cache_file)); 
     } 

    echo json_encode($rss); 

endif; 

?> 
+0

你是什麼意思下,「它沒有被執行」? – ankhzet

+0

嗨!,它是一個Twitter的飼料,它的起源是從特定的Twitter帳戶中檢索推文,但它不工作,推文沒有顯示..我有這個小工具在另一臺服務器上工作,但它不是我的,我不能看到conf。 –

+0

您應該提供您遇到的錯誤消息,如果有的話。就像當你用這個腳本打開頁面一樣,在錯誤控制檯中顯示什麼錯誤?它是否抱怨位置爲'include/twitter/tweets.php?username = coinnova_ok'的未提供腳本?在文檔根目錄下放置在你的nginx服務器上的'include/twitter /'位置? – ankhzet

回答

0

正如從tweets.php可以看出,只有一個點,即可以返回false作爲迴應:

echo json_encode($rss); 

這意味着,$rss變量總是空的,是false或者不能被json編碼。該變量被設定在腳本中的兩行,每尋找相同的:

$rss = @unserialize(file_get_contents($cache_file)); 

這意味着,在文件路徑$cache_file是空的/ unaccesible。這使我們的點,其中該文件被寫入:

$cache_rss = serialize($twitter_feed); 

if (!empty($cache_rss)) { 
    $cache_static = fopen($cache_file, 'wb'); 
    fwrite($cache_static, $cache_rss); 
    fclose($cache_static); 
} 

如果添加以下代碼之前$cache_rss = serialize($twitter_feed)行:

print_r($twitter_feed); 

訪問include/twitter/tweets.php?username=coinnova_ok後,你應該能夠看到大型自卸鳴叫。如果只打印一個值爲false的變量,那麼twitter api的認證/獲取鳴叫存在問題。

另外,如果確實出現大字體打印,那麼將獲取的推文保存到緩存文件時會出現問題。和路徑,文件看起來像:

$cache_file = dirname(__FILE__) . '/cache/' . $username . '_' . $limit; 

因此,在腳本文件夾tweets.php必須有寫文件夾cache,四通八達。

導航到你的服務器上include/twitter/文件夾,創建有cache文件夾(如果需要),並給它一個php腳本(通常07500770寫訪問權限:充分的權利擁有者,FUL /讀/執行業主黨團)。

注意:在所有問題得到解決後,不要記得刪除print_r($twitter_feed);行。