Puneet Arora
asked on
How to save Twitter Post to a MySQL database
Please Guide me how to save tweets related to particular product in MySQL 5.2
Please check Code attached ...
<?php
$q = "Nokia"; // query string
$request = "http://search.twitter.com/search.json?q=".urlencode($q);
$response = file_get_contents($request);
$jsonobj = json_decode($response);
if($jsonobj != null){
$con = mysql_connect('host', 'username', 'password');
if (!$con){
die('Could not connect: ' . mysql_error());
}
foreach($jsonobj->results as $item){
$id = $item->id;
$created_at = $item->created_at;
$created_at = strtotime($created_at);
$mysqldate = date('Y-m-d H:i:s',$created_at);
$from_user = mysql_real_escape_string($item->from_user);
$from_user_id = $item->from_user_id;
$text = mysql_real_escape_string($item->text);
$source = mysql_real_escape_string($item->source);
$geo = $item->geo;
$iso_language_code = $item->iso_language_code;
$profile_image_url = mysql_real_escape_string($item->profile_image_url);
$to_user_id = $item->to_user_id;
if($to_user_id==""){ $to_user_id = 0; }
$query = mysql_real_escape_string($query);
mysql_select_db("database", $con);
// SQL query to create table available at http://snipplr.com/view/56995/sql-query-to-create-a-table-in-mysql-to-store-tweets/
$query = "INSERT into tweets VALUES ($id,'$mysqldate','$from_user',$from_user_id,'$text','$source','$geo','$iso_language_code','$profile_image_url',$to_user_id,'$q')";
$result = mysql_query($query);
}
mysql_close($con);
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The Code is working ,Now probably the twitter site had some issue in responding to queries ..Kindly accept the same code as solution
ASKER
Probably the twitter site had some issue in responding to queries ..Kindly accept the same code as solution
ASKER
I m confused ...