【问题标题】:Get latest tweets through twitter search not working?通过 twitter 搜索获取最新推文不起作用?
【发布时间】:2013-04-28 16:00:41
【问题描述】:

我正在使用以下代码从特定主题标签获取最新推文

$(document).ready(function(){
getLatestTweets();
function getLatestTweets(){
    var url = "http://search.twitter.com/search.json?q=%23syria&result_type=recent&rpp=3&callback=?";

    $.getJSON(url, function(json){
        alert('reached');
        var html = '<ul>';
        $.each(json.results, function( key, result){
            html += '<li><a href="#">' + '> ' + result.text + ' ...' + '</a></li>'; 
        });
        html += '</ul>';
        $('#archive-twitter').append(html);
    });
}
});  

此代码两天前运行良好,但今天停止运行。现在即使我使用以下链接,getJSON 方法也不会成功

http://search.twitter.com/search.json?q=%23syria&result_type=recent&rpp=3

在浏览器中我得到了实际的 json 数据

不知道是什么问题?

更新:我添加了一个测试链接来澄清问题 http://latakiancoder.com/twitter-test.php

【问题讨论】:

  • 您应该升级到现在需要经过身份验证的用户的 API v1.1。似乎 API v1 搜索已停止使用 ajax 请求:dev.twitter.com/blog/planning-for-api-v1-retirement 但我找不到任何有关此的官方公告。
  • 为什么浏览器请求会起作用!!,您能否提供有关使用 jquery 进行身份验证的任何链接或资源,我搜索了他们的文档,但似乎没有找到任何关于 Javascript 身份验证的信息。谢谢。

标签: jquery json twitter tweets


【解决方案1】:

我已经尝试了代理服务器端的请求并且工作正常:

JS代码:

$(document).ready(function(){
                getLatestTweets();
                function getLatestTweets(){
                    var url = "http://search.twitter.com/search.json?q=%23syria&result_type=recent&rpp=3";

                    $.getJSON("proxy.php", { url: url }, function(json){
                        var html = '<ul>';
                        $.each(json.results, function( key, result){
                            html += '<li><a href="#">' + '> ' + result.text + ' ...' + '</a></li>'; 
                        });
                        html += '</ul>';
                        $('#archive-twitter').append(html);
                    });
                }
            });  

proxy.php 代码:

<?php
    $url = $_GET['url'];
    $json = file_get_contents($url);
    echo $json;
?>

【讨论】:

  • 谢谢,我试过了,它在我的本地主机上工作,但是当我将文件上传到服务器时它没有工作。 getJSON“proxy.php”的第一个参数是相对于什么?
  • proxy.php 脚本的相对路径。在这里,只是在服务器根目录。所以如果你把它放在一个文件夹中,只需包含它:myFolderWhereIsProxyScript/proxy.php。顺便说一句,您应该在 php.ini 中允许 allow_url_fopen 或使用 CURL(如果在您的服务器上启用)。
  • 所以我应该把它放在根目录下,不管我的脚本文件存储在哪里?我的网站的另一件事是在共享托管服务上。我无法访问 php.ini 文件,我还有其他选择吗?
  • 只需在 proxy.php 文件夹中创建/添加一个 php.ini 文件就足够了。这个文件应该只包含一行:allow_url_fopen = On。是的,在我的示例中,路径是相对于 index.php,如果您从其他脚本中调用它,则必须使用该脚本中的相对路径或使用绝对路径。
  • 对不起,我不明白你评论的第一句话。你的意思是说我创建了一个新文件夹并将proxy.php和php.ini都放在里面。
【解决方案2】:

您的网址在我这边工作正常。

确保将include_entities 设置为true 以获取主题标签结果。

http://search.twitter.com/search.json?q=ipl&result_type=recent&include_entities=true

查看上面的url响应Header后:

X-Frame-Options SAMEORIGIN
X-XSS-Protection    1; mode=block

好像 twitter 已经停用 API v1。

【讨论】:

  • 是的,我说 url 可以工作,但问题是 getJSON 方法中的代码不会执行。我也尝试了 include_entities,没有任何改变
  • 感谢重播,但是为什么浏览器请求的结果是json数据而我的jquery请求没有结果!!
  • 它检查相同的域策略。通过查看标头似乎跨域 XSS 请求现在被阻止了。
猜你喜欢
  • 1970-01-01
  • 2013-05-30
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 2011-08-29
  • 2018-03-15
相关资源
最近更新 更多