我使用Twitter搜索API即:为什么我无法从twitter查询中获取来自curl_exec的xml文件的标签名称?
$search = "http://search.twitter.com/search.atom?q=" . $q . "";
$tw = curl_init();
curl_setopt($tw, CURLOPT_URL, $search);
curl_setopt($tw, CURLOPT_RETURNTRANSFER, TRUE);
$twi = curl_exec($tw);
$ TW似乎是一个合法的.xml文件。我将它输出到一个文件,它看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:google="http://base.google.com/ns/1.0" xml:lang="en-US" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.w3.org/2005/Atom" xmlns:twitter="http://api.twitter.com/">
<id>tag:search.twitter.com,2005:search/"japan"</id>
<link type="text/html" href="http://search.twitter.com/search?q=%22japan%22" rel="alternate"/>
<link type="application/atom+xml" href="http://search.twitter.com/search.atom?q=%22japan%22" rel="self"/>
<title>"japan" - Twitter Search</title>
<link type="application/opensearchdescription+xml" href="http://search.twitter.com/opensearch.xml" rel="search"/>
<link type="application/atom+xml" href="http://search.twitter.com/search.atom?q=%22japan%22&since_id=53215162768965632" rel="refresh"/>
<twitter:warning>since_id removed for pagination.</twitter:warning>
<updated>2011-03-30T22:00:58Z</updated>
<openSearch:itemsPerPage>15</openSearch:itemsPerPage>
<link type="application/atom+xml" href="http://search.twitter.com/search.atom?max_id=53215162768965632&page=2&q=%22japan%22" rel="next"/>
<entry>
<id>tag:search.twitter.com,2005:53215162768965632</id>
<published>2011-03-30T22:00:58Z</published>
<link type="text/html" href="http://twitter.com/kobe_nowplaying/statuses/53215162768965632" rel="alternate"/>
<title>#NowPlaying "Deora Ar Mo Chroi" by Enya on album "A Day Without Rain (Japan)"</title>
<content type="html"><a href="http://search.twitter.com/search?q=%23NowPlaying" onclick="pageTracker._setCustomVar(2, 'result_type', 'recent', 3);pageTracker._trackPageview('/intra/hashtag/#NowPlaying');">#NowPlaying</a> &quot;Deora Ar Mo Chroi&quot; by Enya on album &quot;A Day Without Rain (<b>Japan</b>)&quot;</content>
<updated>2011-03-30T22:00:58Z</updated>
<link type="image/png" href="http://a1.twimg.com/sticky/default_profile_images/default_profile_0_normal.png" rel="image"/>
<twitter:geo>
</twitter:geo>
<twitter:metadata>
<twitter:result_type>recent</twitter:result_type>
</twitter:metadata>
<twitter:source><a href="http://www.h-fj.com/blog/" rel="nofollow">TWTunes</a></twitter:source>
<twitter:lang>en</twitter:lang>
<author>
<name>kobe_nowplaying (kobe_nowplaying)</name>
<uri>http://twitter.com/kobe_nowplaying</uri>
</author>
</entry>
当我尝试使用AJAX,并得到一个responseXML的背部和使用的getElementsByTagName搜索标签(“入口”),它返回一个 “遗漏的类型错误:不能看空”
的HTML代码的特性 'documentElement':
<button onclick="getTweet('tweetOut.xml')">Get Tweets!</button>
的JS代码:
...
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
x=xmlhttp.responseXML.documentElement.getElementsByTagName("entry");
}
...
程序总是挂在那条JS线上。有任何想法吗?我尝试使用更简单的XML文件,我的代码似乎很好。与curl_exec的输出有什么不同吗?
谢谢。
为什么不使用PHP解析XML并通过AJAX回显输出? – 2011-03-31 22:27:12
@webarto:我认为在原始代码中,作者是这么做的,但是我遇到了一些其他的错误,所以我想从基础开始。我正在使用的教程是从这里:http://papermashup.com/using-the-twitter-search-api/,并在那里,他创建一个新的SimpleXMLElement,然后一些preg_replace,然后一些带标签,并做了一个foreach循环但我遇到了错误,因为我无法调试该PHP文件,我更喜欢只输出XML并解析结果。我可以使用Web检查器来调试JS文件。 – okysabeni 2011-04-01 15:04:22
好的,我复制了我的脚本,这样你就可以看到基础知识。 – 2011-04-01 15:33:36