我从我的Twitter帐户中检索了直接消息,并且我希望将发件人,发件人屏幕名称和文本存储在mysql数据库表中,以便由Artificial intellingece标记语言引擎读取,以便它可以相应地进行回复。执行程序后,我检查了数据库,没有从数组输入任何行,也没有错误消息。
这是我的代码:
$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_name
中的值的数组
和text
。此外,该数组有三个对象,但我只是粘贴在一个例子中。
您怎么看?
Array ( [0] => stdClass Object ( [sender_id] => 374504554 [recipient_screen_name] => Infobot2012 [recipient_id] => 428575951 [recipient] => stdClass Object ( [id] => 428575951 [profile_image_url] => [url] => [created_at] => Sun Dec 04 22:56:38 +0000 2011 [followers_count] => 3 [default_profile] => 1 [profile_background_color] => C0DEED [lang] => en [utc_offset] => [name] => Diego Canale [profile_background_image_url] => [location] => [profile_link_color] => 0084B4 [listed_count] => 0 [verified] => [protected] => [profile_use_background_image] => 1 [is_translator] => [following] => [description] => [profile_text_color] => 333333 [statuses_count] => 10 [screen_name] => Infobot2012 [profile_image_url_https] => [time_zone] => [profile_background_image_url_https] => [friends_count] => 14 [default_profile_image] => 1 [contributors_enabled] => [profile_sidebar_border_color] => C0DEED [id_str] => 428575951 [geo_enabled] => [favourites_count] => 0 [profile_background_tile] => [notifications] => [show_all_inline_media] => [profile_sidebar_fill_color] => DDEEF6 [follow_request_sent] => ) [id_str] => 194550124767227905 [sender] => stdClass Object ( [id] => 374504554 [profile_image_url] => [url] => [created_at] => Fri Sep 16 12:46:20 +0000 2011 [followers_count] => 19 [default_profile] => 1 [profile_background_color] => C0DEED [lang] => en [utc_offset] => [name] => Diego Canale [profile_background_image_url] => [location] => Dublin [profile_link_color] => 0084B4 [listed_count] => 0 [verified] => [protected] => [profile_use_background_image] => 1 [is_translator] => [following] => 1 [description] => [profile_text_color] => 333333 [statuses_count] => 92 [screen_name] => sdiegolo [profile_image_url_https] => [time_zone] => [profile_background_image_url_https] => [friends_count] => 79 [default_profile_image] => [contributors_enabled] => [profile_sidebar_border_color] => C0DEED [id_str] => 374504554 [geo_enabled] => [favourites_count] => 1 [profile_background_tile] => [notifications] => [show_all_inline_media] => [profile_sidebar_fill_color] => DDEEF6 [follow_request_sent] => ) [created_at] => Mon Apr 23 22:15:38 +0000 2012 [id] => 1.9455012476723E+17 [sender_screen_name] => sdiegolo [text] => Hello Infobot! )
答案 0 :(得分:0)
你有没有打电话给你write_messages_to_database()
功能?
无论如何,我建议您按照以下几行重新定义:
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();
}
}
答案 1 :(得分: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());
}