从extJavaScript文件引用PHP


Referencing PHP from an ext JavaScript file

我在尝试使用JavaScript标记引用的extPHP文件在我的网站的标题上获取推文时遇到了一些问题-我在PHP文件中包含了标题引用,最后一行输出的是包含echo的JavaScript。

<?php
Header("content-type: application/x-javascript");
//Get Latest Tweet
function latest_tweet($username,$tweetnumber){
   $url = "http://search.twitter.com/search.atom?q=from:$username&amp;rpp=10";
   $xml = simplexml_load_file($url);
   $tweettitle = $xml->entry[$tweetnumber]->title;
   $mytweet = $xml->entry[$tweetnumber]->content;
   $firstChar = substr($tweettitle, 0, 1);
   //Exclude @ replies
   if($firstChar == "@"){
      //If this tweet is an @ reply move on to the previous one
      while ($firstChar == "@"){
         $tweetnumber++;
         $tweettitle = $xml->entry[$tweetnumber]->title;
         $mytweet = $xml->entry[$tweetnumber]->content;
         $firstChar = substr($tweettitle, 0, 1);
         if($firstChar != "@"){
            //If the previous tweet is not an @ reply output it
            return $mytweet;
         }
      }
   } else {
      //If first tweet is not an @ reply output it
      return $mytweet;
   }
}
//End Get Latest Tweet
//output
echo "document.write(latest_tweet('mikedeveloper', 0))";
?>

由于latest_tweet是一个PHP函数,您必须从PHP调用它,但您正试图从javascript调用它。试试这个:

echo "document.write('" . latest_tweet('mikedeveloper', 0) . "')";