通过hashtag示例API v1.1进行Twitter搜索

时间:2013-06-13 21:26:19

标签: php twitter

过去,使用Twitter API版本1,我使用以下URL来获取所有推文的JSON提要,其中包含#baseball标签:

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

如何使用API​​ 1.1版获得类似的结果?我使用PHP作为我的服务器端代码,所以不确定我是否需要使用它进行身份验证等?

示例代码非常有用。感谢。

3 个答案:

答案 0 :(得分:38)

  

如您所知,现在需要经过身份验证的请求,因此您可能需要先了解一些事项。新的1.1搜索,如何使用主题标签和身份验证。

Twitter搜索1.1

可以找到新的Twitter搜索API文档here。根据这些文档:

https://api.twitter.com/1.1/search/tweets.json是用于搜索的新资源网址。

标签搜索

你有那个部分是正确的! %23解码为#个字符。

<强>验证

OAuth要复杂得多。如果您刚刚使用了一个刚刚工作的库,那将会有所帮助。

以下是a post,很多人发现有助于您向1.1 API发出经过身份验证的请求。这包括一个包含library的单个文件,用于发出您需要的请求。

示例

此示例假设您正在使用上述库并设置密钥等。要提出请求:

// Your specific requirements
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=#baseball&result_type=recent';

// Perform the request
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();

是的,就是这样。除了您需要做的小设置(如我的帖子所解释),对于您的开发键,这是执行经过身份验证的请求所需的一切。

<强>响应

响应以JSON的形式返回给您。来自overview

  

API v1.1仅支持JSON。我们一直在暗示这一点,首先在Streaming API上放弃XML支持,最近在trend API上放弃XML支持。我们选择在平台上共享的JSON格式背后支持。

答案 1 :(得分:4)

如果您只想测试,可以执行以下操作:

访问twitter dev控制台:https://dev.twitter.com/console

在身份验证中:OAuth 1,它会要求您通过Twitter帐户授予权限。

请求网址GET

在网址:https://api.twitter.com/1.1/search/tweets.json?q=%23yourhashtag

发送后,在请求窗口中,复制授权值。

现在把它放在你的请求标题中。

去示例:

func main() {
    client := &http.Client{}
    req, _ := http.NewRequest("GET", "https://api.twitter.com/1.1/search/tweets.json?q=%23golang", nil)
    req.Header.Add("Authorization", `OAuth oauth_consumer_key=...`)

    resp, _ := client.Do(req)
    io.Copy(os.Stdout, resp.Body)
}

答案 2 :(得分:1)

这是python中使用请求API使用仅应用程序身份验证的简单示例。通过在https://apps.twitter.com/app/new创建应用来获取密钥。

public class Main {

    public void setField(PDDocument pdfDocument, String name, String value) throws IOException {
        PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();
        acroForm.getFields().stream().forEach(p -> System.out.println(p.toString()));
        PDField field = acroForm.getField(name);
        System.out.println(field.getFullyQualifiedName());

        COSDictionary dict = ((PDField) field).getCOSObject();
        COSString defaultAppearance = (COSString) dict.getDictionaryObject(COSName.DA);

        if (field != null) {
            if (field instanceof PDCheckBox) {
                field.setValue("Yes");
            } else if (field instanceof PDComboBox) {
                field.setValue(value);
            } else if (field instanceof PDListBox) {
                field.setValue(value);
            } else if (field instanceof PDRadioButton) {
                field.setValue(value);
            } else if (field instanceof PDTextField) {
                field.setValue(value);
            }
            acroForm.flatten();
        } else {
            System.err.println("No field found with name:" + name);
        }

    }

    public static void main(String[] args) throws IOException {
        Main setter = new Main();
        setter.setField(args);
    }

    private void setField(String[] args) throws IOException {
        PDDocument pdf = null;
        try {
            Main example = new Main();
            pdf = PDDocument.load(new File("dmv14.pdf"));
            example.setField(pdf, "first name", "JAT");
            pdf.setAllSecurityToBeRemoved(true);
            pdf.save("dmv14.pdf");
        } finally {
            if (pdf != null) {
                pdf.close();
            }
        }
    }

    /**
     * This will print out a message telling how to use this example.
     */
    private static void usage() {
        System.err.println("usage: org.apache.pdfbox.examples.interactive.form.SetField <pdf-file> <field-name> <field-value>");
    }
}