2011-06-01 116 views
0

这应该是很容易的,但我想我是有点笨....易Twitter的API PHP问题

我想从Twitter获取FULL_NAME:地方 - 任何人都可以帮我吗?为了得到其他元素,我使用$ entry-> title作为例子,但是$ entry-> twitter:places-> full_name不起作用。

<?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></id> 
    <link type="text/html" href="http://search.twitter.com/search?q=" rel="alternate"/> 
    <link type="application/atom+xml" href="http://search.twitter.com/search.atom?q=" rel="self"/> 
    <title>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?&amp;since_id=" rel="refresh"/> 
    <updated>2011-06-01T18:32:50Z</updated> 
    <openSearch:itemsPerPage>15</openSearch:itemsPerPage> 
    <entry> 
    <id></id> 
    <published>2011-06-01T18:32:50Z</published> 
    <link type="text/html" href="" rel="alternate"/> 
    <title></title> 
    <content type="html"></content> 
    <updated>2011-06-01T18:32:50Z</updated> 
    <link type="image/png" href="" rel="image"/> 
    <twitter:geo> 
    </twitter:geo> 
    <twitter:metadata> 
     <twitter:result_type>recent</twitter:result_type> 
    </twitter:metadata> 
    <twitter:place> 
     <twitter:id></twitter:id> 
     <twitter:full_name>London</twitter:full_name> 
     <twitter:type>city</twitter:type> 
    </twitter:place> 
    <twitter:source></twitter:source> 
    <twitter:lang>en</twitter:lang> 
    <author> 
     <name></name> 
     <uri></uri> 
    </author> 
    </entry> 
</feed> 

任何人都可以帮忙吗?

回答

0

为应对前缀的元素(这表明非默认命名空间),你可以通过命名空间过滤:

$name = $xml->entry->children('http://api.twitter.com/')->place->full_name 

或绑定前缀:

$name = $xml->entry->children('twitter', true)->place->full_name 

你的其他选择是使用XPath:

$xml->registerXPathNamespace('t', 'http://api.twitter.com/'); 
$names = $xml->xpath('//t:full_name'); 
$name = $names[0]; 

如果您想使用精确的XPath,则会变得更加复杂,因为有不同的命名空间中的路径:

// Set up our own prefix for default namespace (we could also just use the Atom prefix explicitly as we do below with Twitter) 
$namespaces = $xml->getDocNamespaces(); 
$xml->registerXPathNamespace('atom', $namespaces['']); 
// Set up our own Twitter prefix 
$xml->registerXPathNamespace('t', 'http://api.twitter.com/'); 
$names = $xml->xpath('/atom:feed/atom:entry/t:place/t:full_name'); 
$name = $names[0]; 

XML命名空间前缀通常是任意的 - 最重要的是名称空间(我们结合我们想要的任何前缀,在我们的代码上面的能力所示)。