尝试每隔几个小时运行一个cron作业,读取此处列出的当前Twitter趋势主题:
http://search.twitter.com/trends.json
然后将前10个趋势转储到我服务器上的mySQL表中
怎么做?感谢
答案 0 :(得分:2)
这里有几个可以帮助你的指针:
要使用curl ,您需要initialize一个连接,configure和execute。
例如,像这样的东西可能会(非常基本的例子):
$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获得的全部数据。
将数据插入数据库:
现在,你必须遍历这个'trend'数组,并且,对于每一行,将它插入你的MySQL数据库。
为此,您可以使用:
使用预准备语句也可能有所帮助(mysqli,pdo);-)
如果您没有使用预先准备好的陈述,无论如何,您必须使用mysqli_real_escape_string
或PDO::quote
来逃避您的数据。
在这里,你当然需要有一张桌子,结构正确;我还假设您知道如何在MySQL表中插入数据。
如果您有更具体的问题,请不要犹豫,问一些您使用的代码示例(以及对不起作用的描述/您获得的错误消息,当然)!
玩得开心!
答案 1 :(得分:0)
玩得开心。<?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);
$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);