繁体   English   中英

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

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

尝试每隔几个小时运行一次cron作业,该作业会阅读此处列出的当前Twitter趋势主题:

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

然后将十大趋势转储到服务器上的mySQL表中

这个怎么做? 谢谢

这里有一些指针可以帮助您:


要使用curl,您需要初始化一个连接, 配置它并执行它。

例如,可能会执行以下操作(非常基本的示例)

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

您将获得这种输出:

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)

当然,您可能需要设置其他几个选项; 有关完整列表,请查看curl_setopt的文档。


解析JSON字符串:

如果您使用的是PHP> = 5.2,则可以使用json_decode解析json数据:

$data = json_decode($json);

var_dump($data);

然后,您将获得如下内容:

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)

这包含从twitter获得的全部数据。


将数据插入数据库:

现在,您必须遍历“趋势”数组,并针对每一行将其插入到MySQL数据库中。

为此,您可以使用:

使用准备好的语句也可能有帮助( mysqlipdo );-)
无论如何,如果您没有使用准备好的语句,则必须使用mysqli_real_escape_stringPDO::quote转义数据。

在这里,您当然需要已经有了一个具有正确结构的表格; 我还假设您知道如何在MySQL表中插入数据。


如果您有更具体的问题,请不要犹豫,使用一些您正在使用的不起作用的代码示例(当然还有对不起作用的描述/所得到的错误消息)


玩得开心 !


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

?>

玩得开心。

暂无
暂无

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

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