Benim php function twitter tweets getirir ve json kodlanmış bir dize olarak döndürür. Bir JS komut yakalar veri & Ajax kullanarak bir php komut dosyası için POST. Alıcı php script json çözer ve tabloya eklemek. Ama mysql içine hiçbir veri yoktur. Hata konsol hiçbir hata gösterir. İşte benim kod:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<?php
function searchResults($q) {
$host = "http://search.twitter.com/search.atom?q=" . urlencode( $q ) . "&rpp=100";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//Raw xml
$result = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($result);
return json_encode($xml);
;
} //--------------- end of function
?>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script>
$(document).ready(function()
{
var msg_top = new Array();
msg_top = "<"+"?php echo searchResults('windows');"+"?"+">";
var url = "msg2_mysql.php"
var request = null;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
request=new XMLHttpRequest();
}
else
{// code for IE6, IE5
request=new ActiveXObject("Microsoft.XMLHTTP");
}
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function(){
if (request.readyState == 4) {
if (request.status == 200) {
alert('POST');
} else {
alert(request.status); //
}
}
}
request.send("msg_top=" + encodeURIComponent(msg_top).replace(/%20/g, '+'));
});
</script>
</body>
</html>
Burada tabloya veri eklemek için kullanıyorum script
<?php
$username = "******";
$password = "********";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect
to MySQL");
$selected = mysql_select_db("spec",$dbh) or die("Could not select first_test");
if($_POST['msg_top']!="")
{
$json = $_POST['msg_top'];
$msg = strtoupper(json_decode($json));
$query = "INSERT INTO msg2 (id,msg,msg_id,depth) VALUES ('','$msg','ID','3')";
mysql_query($query);
if(!mysql_query($query, $dbh))
{die('error:' .mysql_error());} echo'success';
}
else echo('no value!');
?>