Jquery 类 php 在 nginx 上不起作用

Jquery calls php on nginx, doesnt work

我在 Amazon AWS 上的 nginx 网络服务器遇到了这个问题。

首先,此代码在另一台服务器上运行并经过测试,但我需要迁移到我自己的服务器,这就是我遇到问题的地方。 我有一个 html 页面,其中包含以下 jquery+php 代码,它没有被执行或类似的代码:

<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;

?>

tweets.php可以看出,只有一点可以return false作为回应:

echo json_encode($rss);

这意味着,$rss 变量始终为空,是 false 或不能被 json 编码。该变量在两行脚本中设置,每行看起来都一样:

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

这意味着,路径 $cache_file 中的文件是 empty/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.

的 authentication/fetching 推文存在问题

否则,如果确实出现大字体,则将提取的推文保存到缓存文件中存在问题。该文件的路径如下所示:

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

因此,在包含脚本 tweets.php 的文件夹中必须有文件夹 cache,可用于写入。

导航到服务器上您的 include/twitter/ 文件夹,在那里创建 cache 文件夹(如果需要),并为其授予 php 脚本(通常 07500770:拥有者的完全权利,ful/read/execute 拥有者组)。

注意:不要记得在所有问题都得到解决后删除 print_r($twitter_feed); 行。