使用html表单输入twitter id,并使用ajax和PHP返回最近的状态
我正在尝试在页面上打印twitter帐户最近的推文,使用html文本feld输入用户id。到目前为止,我可以将用户id发送到PHP页面,该页面获取推文并打印它们 我能够用php连接到twitter api并打印出最近的推文。我需要能够打印推回到我的原始页面。 此外,我还需要ajax不断请求php访问twitter api,以便在用户提交twitter id后,它将在不刷新的情况下更新 以下是链接到php的html表单:使用html表单输入twitter id,并使用ajax和PHP返回最近的状态,php,ajax,twitter,Php,Ajax,Twitter,我正在尝试在页面上打印twitter帐户最近的推文,使用html文本feld输入用户id。到目前为止,我可以将用户id发送到PHP页面,该页面获取推文并打印它们 我能够用php连接到twitter api并打印出最近的推文。我需要能够打印推回到我的原始页面。 此外,我还需要ajax不断请求php访问twitter api,以便在用户提交twitter id后,它将在不刷新的情况下更新 以下是链接到php的html表单: <form name="input" action="twitterT
<form name="input" action="twitterTest2.php" method="get">Twitter Username:
<input type="text" name="userid">
<input type="submit" value="Get recent Tweets">
</form>
Twitter用户名:
以下是从twitter获取推文的php:
<?php
function getTwitterStatus()
{
$userid = $_GET['userid'];
//url that accesses the twitter api for statuses xml
$url = "https://api.twitter.com/1/statuses/user_timeline/$userid.xml?count=5";
$xml = simplexml_load_file($url) or die("could not connect");
foreach ($xml->status as $status) {
$text = $status->text;
echo $text;
}
}
getTwitterStatus();
?>
/**创建跨浏览器XMLHttp请求对象**/
函数getXMLHttp(){
var-xmlhttp;
if(window.ActiveXObject){
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}else if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}否则{
警报(“您的浏览器不支持XMLHTTP!”);
}
返回xmlhttp;
}
//通过php搜索推文的函数
函数getStatuses(){
XMLHttp1=getXMLHttp();
//ajax调用一个php文件来搜索推文
open('GET','twitterTest2.php',true);
//当ajax对象更改其状态时处理数据
ajax.onreadystatechange=函数(){
if(ajax.readyState==4){
如果(ajax.status==200){//则未检测到任何问题
response=XMLHttp.responseText;
document.getElementById(“showText”).value=response;
}
}
}
}
/**检查响应并更新文本区域**/
函数updateInfo(){
如果(xmlhttp1.readyState==4){
response=XMLHttp1.responseText;
document.getElementById(“showText”).value=response;
}
}
将表单替换为AJAX,并使用PHP格式将其转换为JSON,以便AJAX进行解释。不过,我需要将其转换为XML。你能看看下面我的ajax函数吗?我不确定他们出了什么问题。我已经在html中指定了一个“showtext”区域用于打印推文。没关系,我已经解决了。谢谢你的帮助。
<script>
/** Create a cross-browser XMLHttp Request object. **/
function getXMLHttp() {
var xmlhttp;
if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
alert("Your browser does not support XMLHTTP!");
}
return xmlhttp;
}
//function that searches for the tweets via php
function getStatuses(){
XMLHttp1 = getXMLHttp();
//ajax call to a php file that will search the tweets
XMLHttp1.open( 'GET', 'twitterTest2.php', true);
// Process the data when the ajax object changes its state
ajax.onreadystatechange = function() {
if( ajax.readyState == 4 ) {
if ( ajax.status ==200 ) { //no problem has been detected
response = XMLHttp.responseText;
document.getElementById("showText").value = response;
}
}
}
}
/** Check for response and update the text-area. **/
function updateInfo() {
if(xmlhttp1.readyState == 4) {
response=XMLHttp1.responseText;
document.getElementById("showText").value = response;
}
}
</script>