使用html表单输入twitter id,并使用ajax和PHP返回最近的状态


Using a html form to input a twitter id and return the recent statuses using ajax and PHP

我试图在使用html文本字段输入用户id的页面上打印twitter帐户的最近推文。到目前为止,我可以将用户id发送到一个PHP页面,该页面获取tweet并打印它们。

我能够连接到twitter api与php和打印出最近的推文。我需要能够打印推文回到我的原始页面。此外,我需要ajax保持请求php访问twitter api,以便在用户提交twitter id后,它将更新而不刷新。

是链接到php的HTML表单:

<form name="input" action="twitterTest2.php" method="get">Twitter Username: 
  <input type="text" name="userid">
  <input type="submit" value="Get recent Tweets">
</form>

这里是PHP从twitter获取tweets:

<?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();
?> 
<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>