为什么我无法从 twitter 查询的curl_exec 获取xml 文件的标签名称?
我正在使用 twitter 搜索 API,即:
$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("entry") 搜索标签时,它返回一个 “Uncaught TypeError: Cannot read property 'documentElement' of null”
HTML 代码:
<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 的输出有什么不同吗?
谢谢。
I am using the twitter search API namely:
$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 seems like a legit .xml file. I output it to a file and it looks like this:
<?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> "Deora Ar Mo Chroi" by Enya on album "A Day Without Rain (<b>Japan</b>)"</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>
When I try to use AJAX and get a responseXML back and search for tags using getElementsByTagName("entry") it returns a
"Uncaught TypeError: Cannot read property 'documentElement' of null"
The HTML Code:
<button onclick="getTweet('tweetOut.xml')">Get Tweets!</button>
The JS code:
...
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
x=xmlhttp.responseXML.documentElement.getElementsByTagName("entry");
}
...
The program always hangs at that JS line. Any ideas? i tried using a much simpler XML file and my code seems to be fine. Is there something different with the output from curl_exec?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结果(呈现的 HTML)...
Result (rendered HTML)...
您是否尝试过在没有第一行的情况下使用相同的 XML
或者尝试:
Did you try the same XML without first row
<?xml version="1.0" encoding="UTF-8"?>
Or try: