2012-03-07 44 views
-1

我在尝试从我的网站的标题中获取我的推文时使用了一个来自JavaScript标记的ext PHP文件 - 我在PHP文件中包含了标题引用,输出为最后一行是javascript包含回声。从ext JavaScript文件引用PHP

<?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))"; 
?> 
+2

'latest_tweet'是一个PHP函数,而不是一个JavaScript函数。这种方式不能用JavaScript调用PHP函数。你为什么不直接用PHP输出内容? 'echo latest_tweet('mikedeveloper',0);'。 – 2012-03-07 21:02:31

+0

我想让我的JavaScript和PHP独立于标记。 – 2012-03-07 21:12:57

+2

但是'document.write(...)'似乎是不必要的......我想这取决于你如何包含这个文件。如果在文档加载后调用document.write,它将覆盖当前文档。 – 2012-03-07 21:16:23

回答

1

由于latest_tweet是一个PHP函数,你必须从PHP调用它,但是你想从JavaScript调用。试试这个:

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

啊谢谢你!上周才刚刚开始使用PHP,但您一直很有帮助。 – 2012-03-07 21:05:30

+1

请考虑@Felix Klink为您的问题添加的评论。尝试通过引用'