2012-03-14 89 views
0

我想下载推特到我自己的数据库。我想给脚本中的#subject,然后下载最新的推文。我想这样做与PHP存储推文位置信息

我只能找到网站,您可以监控它,但我需要一个脚本。有人知道吗?

重要的是,我可以下载消息,作者和位置。 我不熟悉Twitter,所以我不确定这是否可能。

感谢,

托比

+1

“我想给脚本中的#subject”。你的意思是你想给查询并根据你给的关键字得到结果吗? – Zakaria 2012-03-14 15:11:13

回答

1

这将涉及使用它作为一个RESTful Web服务实现的Twitter API。主要你会使用Twitter search API

使用Twitter的API和搜索API: 你首先需要的是你的搜索查询,如果你不知道的它然后使用search page。以搜索网址结束并将其用作搜索API的参数。例如,搜索hashtag #stackoverflow会为我们提供以下URL。

http://twitter.com/#!/search/%23stackoverflow 

对于搜索API的查询将如下所示。

http://search.twitter.com/search.json?q=%23stackoverflow 

上面的URL返回一个JSON响应,您可以在您的PHP脚本中解析和使用该响应。

用PHP解析搜索: 与Twitter的API此脚本接口,并打印出结果,将结果存储是一个练习留给读者:

<?php 
class Twitter { 
    public function __construct() { } 
    public function searchResults($query = null) { 
     $url = "http://search.twitter.com/search.json?q=" . urlencode($query); 
     $curl = curl_init(); 

     // cURL options for the current session. 
     $options = array(CURLOPT_URL => $url, 
         CURLOPT_RETURNTRANSFER => 1 
        ); 
     // If all the options are set correctly then fetch and parse the results. 
      if (curl_setopt_array($curl, $options)) { 
       // Execute cURL and store the returned string. 
       $json_search_results = curl_exec($curl); 
       curl_close($curl); 
       // Decode the JSON string returned by cURL. 
       $search_results = json_decode($json_search_results); 
       return $search_results; 
      } 
    } 
} 

// Searching Twittter using the new class. 
$Twitter = new Twitter(); 
$search = Twitter::searchResults("#stackoverflow"); 
var_dump($search); 
?> 

脚本的输出:

object(stdClass)#2 (11) { 
    ["completed_in"]=> 
    float(0.137) 
    ["max_id"]=> 
    int(179961163788976129) 
    ["max_id_str"]=> 
    string(18) "179961163788976129" 
    ["next_page"]=> 
    string(52) "?page=2&max_id=179961163788976129&q=%23stackoverflow" 
    ["page"]=> 
    int(1) 
    ["query"]=> 
    string(16) "%23stackoverflow" 
    ["refresh_url"]=> 
    string(47) "?since_id=179961163788976129&q=%23stackoverflow" 
    ["results"]=> 
    array(15) { 
    [0]=> 
    object(stdClass)#3 (18) { 
     ["created_at"]=> 
     string(31) "Wed, 14 Mar 2012 16:04:19 +0000" 
     ["from_user"]=> 
     string(7) "kel666_" 
     ["from_user_id"]=> 
     int(55252171) 
     ["from_user_id_str"]=> 
     string(8) "55252171" 
     ["from_user_name"]=> 
     string(14) "Fabio Spinelli" 
     ["geo"]=> 
     NULL 
     ["id"]=> 
     int(179961163788976129) 
     ["id_str"]=> 
     string(18) "179961163788976129" 
     ["iso_language_code"]=> 
     string(2) "it" 
     ["metadata"]=> 
     object(stdClass)#4 (1) { 
     ["result_type"]=> 
     string(6) "recent" 
     } 
     ["profile_image_url"]=> 
     string(71) "http://a0.twimg.com/profile_images/585239857/n571218917_3111_normal.jpg" 
     ["profile_image_url_https"]=> 
     string(73) "https://si0.twimg.com/profile_images/585239857/n571218917_3111_normal.jpg" 
     ["source"]=> 
     string(59) "&lt;a href=&quot;http://twitter.com/&quot;&gt;web&lt;/a&gt;" 
     ["text"]=> 
     string(114) "postare domande su #stackoverflow è fico perché c'è sempre pronto qualcuno a cazziarti o cmq a far la maestrina" 
     ["to_user"]=> 
     NULL 
     ["to_user_id"]=> 
     NULL 
     ["to_user_id_str"]=> 
     NULL 
     ["to_user_name"]=> 
     NULL 
    } 
    // Snip. 
    ["results_per_page"]=> 
    int(15) 
    ["since_id"]=> 
    int(0) 
    ["since_id_str"]=> 
    string(1) "0" 
} 
+0

谢谢我一直在寻找这个 – 2012-03-15 11:44:20