0

我已经从我的 Twitter 帐户中检索了直接消息,并且我想将发件人 ID、发件人屏幕名称和文本存储在 mysql 数据库表中,以便人工智能标记语言引擎读取它,以便它可以做出相应的回复。执行程序后,我检查了数据库,没有从数组中输入任何行,也没有错误消息。

这是我的代码:

$connection = new TwitterOAuth($consumer_key,$consumer_secret, $oAuthToken, $oAuthSecret);

// Get direct message data 
$content = $connection ->get('https://api.twitter.com/1/direct_messages.json');

echo '<pre>',print_r($content,true),'</pre>';

function write_messages_to_database() {

require_once('connect_to_database');

$conn = mysql_connect("127.0.0.1", "Diego", "frafra") or die(mysql_error());
mysql_select_db('bot', $conn) or die(mysql_error());

foreach ($content as $directmessage) {
   mysql_query("INSERT INTO 'twitter_senders' ('sender_id', 'sender_name'), VALUES ('{$content->id}', '{$content->sender_screen_name}')");
   $directmessage_id = mysql_last_insert_id();
   foreach ($directmessage->text as $text) {
        mysql_query("INSERT INTO 'message_senders' ('message'), VALUES ('{$text->text}')");
   } 

 }
}

下面,我只想从中提取 和 中的值 的id数组。此外,该数组有三个对象,但我只是粘贴了一个作为示例。sender_screen_nametext

你怎么看?

大批
(
    [0] => 标准类对象
        (
            [sender_id] => 374504554
            [recipient_screen_name] => Infobot2012
            [recipient_id] => 428575951
            [收件人] => stdClass 对象
                (
                    [id] => 428575951
                    [profile_image_url] =>
                    [网址] =>
                    [created_at] => 2011 年 12 月 4 日星期日 22:56:38 +0000
                    [followers_count] => 3
                    [default_profile] => 1
                    [profile_background_color] => C0DEED
                    [语言] => zh
                    [utc_offset] =>
                    [名称] => 迭戈运河
                    [profile_background_image_url] =>
                    [位置] =>
                    [profile_link_color] => 0084B4
                    [listed_count] => 0
                    [已验证] =>
                    [受保护] =>
                    [profile_use_background_image] => 1
                    [is_translator] =>
                    [关注] =>
                    [描述] =>
                    [profile_text_color] => 333333
                    [statuses_count] => 10
                    [screen_name] => Infobot2012
                    [profile_image_url_https] =>
                    [时区] =>
                    [profile_background_image_url_https] =>
                    [friends_count] => 14
                    [default_profile_image] => 1
                    [contributors_enabled] =>
                    [profile_sidebar_border_color] => C0DEED
                    [id_str] => 428575951
                    [geo_enabled] =>
                    [收藏夹数] => 0
                    [profile_background_tile] =>
                    [通知] =>
                    [show_all_inline_media] =>
                    [profile_sidebar_fill_color] => DDEEF6
                    [follow_request_sent] =>
                )

            [id_str] => 194550124767227905
            [发件人] => 标准类对象
                (
                    [id] => 374504554
                    [profile_image_url] =>
                    [网址] =>
                    [created_at] => 2011 年 9 月 16 日星期五 12:46:20 +0000
                    [followers_count] => 19
                    [default_profile] => 1
                    [profile_background_color] => C0DEED
                    [语言] => zh
                    [utc_offset] =>
                    [名称] => 迭戈运河
                    [profile_background_image_url] =>
                    [位置] => 都柏林
                    [profile_link_color] => 0084B4
                    [listed_count] => 0
                    [已验证] =>
                    [受保护] =>
                    [profile_use_background_image] => 1
                    [is_translator] =>
                    [以下] => 1
                    [描述] =>
                    [profile_text_color] => 333333
                    [statuses_count] => 92
                    [screen_name] => sdiegolo
                    [profile_image_url_https] =>
                    [时区] =>
                    [profile_background_image_url_https] =>
                    [friends_count] => 79
                    [default_profile_image] =>
                    [contributors_enabled] =>
                    [profile_sidebar_border_color] => C0DEED
                    [id_str] => 374504554
                    [geo_enabled] =>
                    [收藏夹数] => 1
                    [profile_background_tile] =>
                    [通知] =>
                    [show_all_inline_media] =>
                    [profile_sidebar_fill_color] => DDEEF6
                    [follow_request_sent] =>
                )

            [created_at] => 2012 年 4 月 23 日星期一 22:15:38 +0000
            [id] => 1.9455012476723E+17
            [sender_screen_name] => sdiegolo
            [文本] => 你好信息机器人!
        )
4

2 回答 2

0
  1. 你有没有调用你的write_messages_to_database()函数?

  2. 无论如何,我建议您按照以下方式重新定义它:

    function write_messages_to_database($content) {
        // Use PDO - it'll make your life so much easier
        $dbh = new PDO('mysql:dbname=bot;host=127.0.0.1', "Diego", "frafra");
    
        // use prepared statements to prevent SQL injection
        // see http://bobby-tables.com for more info
        // great thing is, we can prepare once and use many!
        $sender_stmt = $dbh->prepare("
            INSERT INTO twitter_senders (sender_id, sender_name)
            VALUES (:id, :name)
        ");
        $sender_stmt->bindParam(":id"  , $sender_id  );
        $sender_stmt->bindParam(":name", $sender_name);
    
        $message_stmt = $dbh->prepare("
            INSERT INTO message_senders (message) VALUES (:text)
        ");
        $message_stmt->bindParam(":text", $text);
    
        foreach ($content as $directmessage) {
           $sender_id   = $directmessage->sender->id  ;
           $sender_name = $directmessage->sender->name;
           $sender_stmt->execute();
    
           // this is pointless...
           $directmessage_id = $dbh->lastInsertId();
    
           $text = $directmessage->text;
           $message_stmt->execute();
        }
    }
    
于 2012-04-27T00:07:26.913 回答
0

除非我们知道错误是什么,否则无法帮助您。

尝试这样做:

foreach ($directmessage->text as $text) {
        mysql_query("INSERT INTO 'message_senders' ('message'), VALUES ('{$text->text}')") 
        or die(mysql_error());
       } 

此外,更好的方法是:

foreach ($directmessage->text as $text) {
    $query = sprintf("INSERT INTO 'message_senders' ('message'), VALUES ('%s')"
                , mysql_real_escape_string($text->text));
    mysql_query($query) or die(mysql_error());
} 
于 2012-04-27T00:14:39.390 回答