twitter api 的 JSON HTTP 请求示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8890257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:08:36  来源:igfitidea点击:

Example JSON HTTP request for twitter api?

jsontwitter

提问by lisovaccaro

I want to make a request to the twitter api. This is the example provided in the documentation (https://dev.twitter.com/docs/api/1/get/search):

我想向 twitter api 发出请求。这是文档(https://dev.twitter.com/docs/api/1/get/search)中提供的示例:

GET:

http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed

There is no example request on the documentation. How would the request for this url be including an alert with data response?

文档中没有示例请求。对此 url 的请求将如何包含带有数据响应的警报?

回答by WoLfulus

Look if this helps, I made an example for you:

看看这是否有帮助,我为你做了一个例子:

Basically the HTML code contains 2 inputs. one for the button and one for the query string.

基本上 HTML 代码包含 2 个输入。一个用于按钮,一个用于查询字符串。

<html>
<head>
    <title>example</title>
</head>
<body>
   <div style="padding: 20px;">
        <input id="query" type="text" value="blue angels" />
        <input id="submit" type="button" value="Search" />
    </div>
    <div id="tweets" style="padding: 20px;">
        Tweets will go here.
    </div>
</body>
</html>

After pressing the search button, you'll send a request to twitter asking for 5 results (rpp) containing the query string.

按下搜索按钮后,您将向 twitter 发送请求,要求包含查询字符串的 5 个结果 (rpp)。

Here's the javascript for this page:

这是该页面的 javascript:

function searchTwitter(query) {
    $.ajax({
        url: 'http://search.twitter.com/search.json?' + jQuery.param(query),
        dataType: 'jsonp',
        success: function(data) {
            var tweets = $('#tweets');
            tweets.html('');
            for (res in data['results']) {
                tweets.append('<div>' + data['results'][res]['from_user'] + ' wrote: <p>' + data['results'][res]['text'] + '</p></div><br />');
        }
        }
    });
}

$(document).ready(function() {
    $('#submit').click(function() {
        var params = {
            q: $('#query').val(),
            rpp: 5
        };
        // alert(jQuery.param(params));
        searchTwitter(params);
    });
});

The trick is the jQuery.param() function that you'll pass the params for the search/request

诀窍是 jQuery.param() 函数,您将为搜索/请求传递参数

See it running here:

看到它在这里运行:

http://jsfiddle.net/73L4c/6/

http://jsfiddle.net/73L4c/6/

回答by Niki

This does not work anymore since Twitter announced API 1.1. Look at this question to learn to use API 1.1: Need help converting to Twitter API v1.1 - JavaScript

自 Twitter 宣布 API 1.1 以来,这不再起作用。看这个问题来学习使用 API 1.1:需要帮助转换到 Twitter API v1.1 - JavaScript