0

ユーザーIDを入力するためにHTMLテキストフィールドを使用して、Twitterアカウントの最近のツイートをページに印刷しようとしています。これまでのところ、ツイートを取得して印刷する PHP ページにユーザー ID を送信できます。

php を使用して Twitter API に接続し、最近のツイートを出力できます。ツイートを元のページに印刷できるようにする必要があります。また、ユーザーがtwitter idを送信した後、更新せずに更新されるように、phpにtwitter apiにアクセスするように要求し続けるにはajaxが必要です。

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>

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

1 に答える 1

0
<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>
于 2013-04-08T15:45:42.033 に答える