xml:
<entry> <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://picasaweb.google.com/data/feed/api/user/xy/albumid/531885671007533108" /> <link rel="alternate" type="text/html" href="http://picasaweb.google.com/xy/Cooking" /> <link rel="self" type="application/atom+xml" href="http://picasaweb.google.com/data/entry/api/user/xy/albumid/531885671007533108" /> </entry>
Ecco cosa ho provato:
foreach($xml->entry as $feed) { $album_url = $feed->xpath("./link[@rel='alternate']/@href"); echo $album_url; }
Ho provato anche tutti i tipi di permutazioni, ma senza fortuna.
Il risultato previsto sarebbe http://picasaweb.google.com/xy/Cooking
Il risultato che ottengo è "". Qualcuno può spiegare cosa sto facendo male?
Qualcuno può aiutarmi? Sono stato a questo per ore …
xpath()
restituisce un arrays, devi select il primo elemento di quell'arrays, all'indice 0. Attenzione: se non c'è corrispondenza, può restituire un arrays vuoto. Pertanto, è necessario aggiungere una clausola if (isset($xpath[0]))
, per each evenienza.
foreach ($xml->entry as $entry) { $xpath = $entry->xpath('./link[@rel="alternate"]/@href'); if (isset($xpath[0])) { echo $xpath[0], "\n"; } }
Eri vicino:
./link[@rel='alternate']/@href
Dovrebbe essere l'XPath corretto per get quei valori.