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

发布于 2024-10-29 01:11:08 字数 3834 浏览 1 评论 0原文

我正在使用 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") 搜索标签时,它返回一个 “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> &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>

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-11-05 01:11:08
$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)
$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);
}

Result (rendered HTML)...

# test #stackoverflow (01.04.2011)
好久不见√ 2024-11-05 01:11:08

您是否尝试过在没有第一行的情况下使用相同的 XML

或者尝试:

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

Did you try the same XML without first row <?xml version="1.0" encoding="UTF-8"?>

Or try:

x=xmlhttp.responseXML.documentElement.getElementsByTagName("entry")[0]; //I think this will return you first entry
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文