简体   繁体   English

如何使用cURL / PHP将当前的Twitter趋势主题转储到MySQL中

[英]How do I use cURL/PHP to dump the current twitter trending topics into mySQL

Trying to run a cron job every few hours that reads the current Twitter Trending topics listed here: 尝试每隔几个小时运行一次cron作业,该作业会阅读此处列出的当前Twitter趋势主题:

http://search.twitter.com/trends.json http://search.twitter.com/trends.json

And then dump the top 10 trends into a mySQL table on my server 然后将十大趋势转储到服务器上的mySQL表中

how to do this? 这个怎么做? thanks 谢谢

Here a couple of pointers that could help you : 这里有一些指针可以帮助您:


To use curl : you will need to initialize a connexion, configure it, and execute it. 要使用curl,您需要初始化一个连接, 配置它并执行它。

For instance, something like this might do (very basic example) : 例如,可能会执行以下操作(非常基本的示例)

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://search.twitter.com/trends.json");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$json = curl_exec($ch);

curl_close($ch);

// $json contains the data you want
var_dump($json);

You will get that kind of output : 您将获得这种输出:

string '{"as_of":"Fri, 21 Aug 2009 20:59:54 +0000","trends":[{"name":"Follow Friday","url":"http:\/\/search.twitter.com\/search?q=%22Follow+Friday%22"},{"name":"#BrutalLegend","url":"http:\/\/search.twitter.com\/search?q=%23BrutalLegend"},{"name":"#shoutout","url":"http:\/\/search.twitter.com\/search?q=%23shoutout"},{"name":"#fact","url":"http:\/\/search.twitter.com\/search?q=%23fact"},{"name":"Inglourious","url":"http:\/\/search.twitter.com\/search?q=Inglourious"},{"name":"Which Horror Movie","url":"http:\/\/search.twitter.com\/search?q=%22Which+Horror+Movie%22"},{"name":"Cataclysm","url":"http:\/\/search.twitter.com\/search?q=Cataclysm"},{"name":"Inglourious Basterds","url":"http:\/\/search.twitter.com\/search?q=%22Inglourious+Basterds%22+OR+%22Inglorious+Basterds%22"},{"name":"District 9","url":"http:\/\/search.twitter.com\/search?q=%22District+9%22"},{"name":"Twitter Besides","url":"http:\/\/search.twitter.com\/search?q=%22Twitter+Besides%22"}]}' (length=955)

Of course, you might want to set a couple of other options ; 当然,您可能需要设置其他几个选项; for the complete list, take a look at the documentation of curl_setopt . 有关完整列表,请查看curl_setopt的文档。


Parsing the JSON string : 解析JSON字符串:

Provided you are using PHP >= 5.2, you can use json_decode to parse the json data : 如果您使用的是PHP> = 5.2,则可以使用json_decode解析json数据:

$data = json_decode($json);

var_dump($data);

You will then get something like this : 然后,您将获得如下内容:

object(stdClass)[1]
  public 'as_of' => string 'Fri, 21 Aug 2009 21:01:48 +0000' (length=31)
  public 'trends' => 
    array
      0 => 
        object(stdClass)[2]
          public 'name' => string 'Follow Friday' (length=13)
          public 'url' => string 'http://search.twitter.com/search?q=%22Follow+Friday%22' (length=54)
      1 => 
        object(stdClass)[3]
          public 'name' => string '#BrutalLegend' (length=13)
          public 'url' => string 'http://search.twitter.com/search?q=%23BrutalLegend' (length=50)
      2 => 
        object(stdClass)[4]
          public 'name' => string '#shoutout' (length=9)
          public 'url' => string 'http://search.twitter.com/search?q=%23shoutout' (length=46)
      3 => 
        object(stdClass)[5]
          public 'name' => string '#fact' (length=5)
          public 'url' => string 'http://search.twitter.com/search?q=%23fact' (length=42)
      4 => 
        object(stdClass)[6]
          public 'name' => string 'Inglourious' (length=11)
          public 'url' => string 'http://search.twitter.com/search?q=Inglourious' (length=46)
      5 => 
        object(stdClass)[7]
          public 'name' => string 'Cataclysm' (length=9)
          public 'url' => string 'http://search.twitter.com/search?q=Cataclysm' (length=44)
      6 => 
        object(stdClass)[8]
          public 'name' => string 'Which Horror Movie' (length=18)
          public 'url' => string 'http://search.twitter.com/search?q=%22Which+Horror+Movie%22' (length=59)
      7 => 
        object(stdClass)[9]
          public 'name' => string 'Inglourious Basterds' (length=20)
          public 'url' => string 'http://search.twitter.com/search?q=%22Inglourious+Basterds%22+OR+%22Inglorious+Basterds%22' (length=90)
      8 => 
        object(stdClass)[10]
          public 'name' => string 'District 9' (length=10)
          public 'url' => string 'http://search.twitter.com/search?q=%22District+9%22' (length=51)
      9 => 
        object(stdClass)[11]
          public 'name' => string 'Hurricane Bill' (length=14)
          public 'url' => string 'http://search.twitter.com/search?q=%22Hurricane+Bill%22' (length=55)

This contains the whole data obtained from twitter. 这包含从twitter获得的全部数据。


Inserting the data into database : 将数据插入数据库:

Now, you have to iterate over this array of 'trends', and, for each line, insert it into your MySQL database. 现在,您必须遍历“趋势”数组,并针对每一行将其插入到MySQL数据库中。

For this, you can use either : 为此,您可以使用:

Using prepared statements might help, too ( mysqli , pdo ) ;-) 使用准备好的语句也可能有帮助( mysqlipdo );-)
If you are not using prepared statements, anyway, you must thing about escaping your data, using either mysqli_real_escape_string or PDO::quote . 无论如何,如果您没有使用准备好的语句,则必须使用mysqli_real_escape_stringPDO::quote转义数据。

Here, you will of course need to already have a table, with the right structure ; 在这里,您当然需要已经有了一个具有正确结构的表格; I also suppose you know how to insert data in a MySQL table. 我还假设您知道如何在MySQL表中插入数据。


If you have more specific questions, don't hesitate to ask, with some examples of code you are using that doesn't work (and a description of what doesn't work / the error message you are getting, of course) ! 如果您有更具体的问题,请不要犹豫,使用一些您正在使用的不起作用的代码示例(当然还有对不起作用的描述/所得到的错误消息)


Have fun ! 玩得开心 !


<?php

$curl = curl_init();
curl_setopt_array($curl, Array(
    /* CURLOPT_HEADER         => true,
    CURLOPT_POSTFIELDS     => $postdata,
    CURLOPT_POST           => $method == 'POST',
    CURLOPT_HTTPHEADER     => $headers,
    CURLOPT_USERAGENT      => "Mozil.....", */
    CURLOPT_URL            => "http://search.twitter.com/trends.json",
    CURLOPT_TIMEOUT        => 300,
    CURLOPT_CONNECTTIMEOUT => 60,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_ENCODING       => 'gzip,deflate'
));
$json = curl_exec($curl);
$json = json_decode($json);
echo "<pre>";
print_r($json);

?>

Have fun. 玩得开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM