2011-06-01 70 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命名空間前綴通常是任意的 - 最重要的是名稱空間(我們結合我們想要的任何前綴,在我們的代碼上面的能力所示)。