Inserting data from Twitter API into a MySQL Database using php -
very new php coding - i've set server based mysql database insert twitter api data into.
i can retrieve twitter api data , profile on web (http://www.pdanalytics.ca/stephenharpertweets.php). using php, how insert database.
i've googled, , made attempts based on general tutorials, still feel pretty far off believe should straight forward task.
thanks.
here code:
<?php require_once('twitterapiexchange.php'); /** set access tokens here - see: https://dev.twitter.com/apps/ **/ $settings = array( 'oauth_access_token' => "xxxxxxxxxxxxxxx", 'oauth_access_token_secret' => "xxxxxxxxxxxxxxx", 'consumer_key' => "xxxxxxxxxxxxx", 'consumer_secret' => "xxxxxxxxxxxxxxxxxxx" ); $url = "https://api.twitter.com/1.1/statuses/user_timeline.json"; $requestmethod = "get"; $getfield = '?screen_name=pmharper&count=20'; $twitter = new twitterapiexchange($settings); $string = json_decode($twitter->setgetfield($getfield) ->buildoauth($url, $requestmethod) ->performrequest(),$assoc = true); if($string["errors"][0]["message"] != "") {echo "<h3>sorry, there problem.</h3><p>twitter returned following error message:</p><p> <em>".$string[errors][0]["message"]."</em></p>";exit();} foreach($string $items) { echo "tweeted by: ". $items['user']['name']."<br />"; echo "screen name: ". $items['user']['screen_name']."<br />"; echo "tweet: ". $items['text']."<br />"; echo "time , date of tweet: ".$items['created_at']."<br />"; echo "tweet id: ".$items['id_str']."<br />"; echo "followers: ". $items['user']['followers_count']."<br /><hr />"; } ?>
here final code!
i used mysqli prepared statement insert query. hope created database , table inserting tweets. need change first few lines of below code , start using it.
<?php define('dbhost','localhost'); define('dbusername','databaseusername'); define('dbpassword','databasepassword'); define('dbname','databasename'); define('tweettable','tweettablename'); require_once('twitterapiexchange.php'); /** set access tokens here - see: https://dev.twitter.com/apps/ **/ $settings = array( 'oauth_access_token' => "xxxxxxxxxxxxxxx", 'oauth_access_token_secret' => "xxxxxxxxxxxxxxx", 'consumer_key' => "xxxxxxxxxxxxx", 'consumer_secret' => "xxxxxxxxxxxxxxxxxxx" ); $url = "https://api.twitter.com/1.1/statuses/user_timeline.json"; $requestmethod = "get"; $getfield = '?screen_name=pmharper&count=20'; $twitter = new twitterapiexchange($settings); $string = json_decode($twitter->setgetfield($getfield) ->buildoauth($url, $requestmethod) ->performrequest(),$assoc = true); if($string["errors"][0]["message"] != "") {echo "<h3>sorry, there problem.</h3><p>twitter returned following error message:</p><p> <em>".$string[errors][0]["message"]."</em></p>";exit();} foreach($string $items) { echo "tweeted by: ". $items['user']['name']."<br />"; echo "screen name: ". $items['user']['screen_name']."<br />"; echo "tweet: ". $items['text']."<br />"; echo "time , date of tweet: ".$items['created_at']."<br />"; echo "tweet id: ".$items['id_str']."<br />"; echo "followers: ". $items['user']['followers_count']."<br /><hr />"; echo inserttweets($items['user']['name'],$items['user']['screen_name'],$items['text'],$items['created_at'],$items['id_str'],$items['user']['followers_count']); } function inserttweets($name,$screen_name,$text,$created_at,$id_str,$followers_count){ $mysqli = new mysqli(dbhost, dbusername, dbpassword, dbname); if ($mysqli->connect_errno) { return 'failed connect database: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error; } $preparestmt='insert '.dbname.'.'.tweettable.' (name, screen_name, text, created_at, id_str, followers_count) values (?,?,?,?,?,?);'; if ($insert_stmt = $mysqli->prepare($preparestmt)){ $insert_stmt->bind_param('ssssid', $name,$screen_name,$text,$created_at,$id_str,$followers_count); if (!$insert_stmt->execute()) { $insert_stmt->close(); return 'tweet creation cannot done @ moment.'; }elseif($insert_stmt->affected_rows>0){ $insert_stmt->close(); return 'tweet added.'; }else{ $insert_stmt->close(); return 'no tweet added.'; } }else{ return 'prepare failed: (' . $mysqli->errno . ') ' . $mysqli->error; } } ?>
Comments
Post a Comment