为什么我不能从twitter查询中获取curl_exec的xml文件的标签名称?

时间:2011-03-31 22:23:03

标签: javascript dom namespaces

我正在使用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/&quot;japan&quot;</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>&quot;japan&quot; - 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&amp;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&amp;page=2&amp;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 &quot;Deora Ar Mo Chroi&quot; by Enya on album &quot;A Day Without Rain (Japan)&quot;</title>
    <content type="html">&lt;a href=&quot;http://search.twitter.com/search?q=%23NowPlaying&quot; onclick=&quot;pageTracker._setCustomVar(2, 'result_type', 'recent', 3);pageTracker._trackPageview('/intra/hashtag/#NowPlaying');&quot;&gt;#NowPlaying&lt;/a&gt; &amp;quot;Deora Ar Mo Chroi&amp;quot; by Enya on album &amp;quot;A Day Without Rain (&lt;b&gt;Japan&lt;/b&gt;)&amp;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>&lt;a href=&quot;http://www.h-fj.com/blog/&quot; rel=&quot;nofollow&quot;&gt;TWTunes&lt;/a&gt;</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”)搜索标签时,它返回一个 “未捕获的TypeError:无法读取属性'documentElement'的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的输出有什么不同吗?

感谢。

3 个答案:

答案 0 :(得分:1)

$search = "webarto";

$feed = curl("http://search.twitter.com/search.atom?q=$search");

$tags = array("title", "name", "uri", "published");
foreach($tags as $tag){
    $atom["$tag"] = matchTags("$tag", $feed);
}
unset($atom["title"][0]);
$atom["title"] = array_values($atom["title"]);

for($i = 0; $i < count($atom["title"]); $i++){
    $atom["title"][$i] = htmlentities($atom["title"][$i], ENT_QUOTES);
    $atom["title"][$i] = preg_replace("/http\:\/\/(.*?)\s/",'<a rel="nofollow" target="_blank" href="http://\\1">http://\\1</a> ',$atom["title"][$i]);
    $atom["published"][$i] = date("d.m.Y", strtotime($atom["published"][$i]));
    echo('<li><a rel="nofollow" target="_blank" href="'.$atom["uri"][$i].'">'.$atom["title"][$i].' ('.$atom["published"][$i].')</a></li>');
}

function matchTags($tag, $xml){
    preg_match_all("#\<$tag\>(.*?)\<\/$tag\>#is", $xml, $bingo);
    return $bingo[1];
}
function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_close ($ch);
    return curl_exec($ch);
}

结果(呈现的HTML)...

# test #stackoverflow (01.04.2011)

答案 1 :(得分:0)

您是否在没有第一行<?xml version="1.0" encoding="UTF-8"?>

的情况下尝试使用相同的XML

或尝试:

x=xmlhttp.responseXML.documentElement.getElementsByTagName("entry")[0]; //I think this will return you first entry

答案 2 :(得分:0)

您可能应该等待加载文档: 像这样:

xmlhttp.onreadystatechange=onResponse;
function onResponse() {
    x=xmlhttp.responseXML.documentElement.getElementsByTagName("entry");
}