Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract the "href" attribute from an XML "link" tag using PHP?

I am stumped as to how I can extract the "href" attribute from the "link" tag from this bit of XML using my PHP parsing script. If it helps at all, I am trying to extract the URL of a particular post from a GetSatisfaction API feed.

Here is an example a node from the XML file:

<entry>
  <link rel="something" href="http://...url_I_need" type="text/html"/>

  <title type="html">...title here...</title>
  <content type="html">
  ...content here...
  </content>
</entry>

And here is the data gathering portion of my PHP XML parsing script:

$doc = new DOMDocument();
$doc->load('http://api.getsatisfaction.com/companies/issuetrak/topics?sort=recently_active&limit=7');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('entry') as $node) {
 $title = $node->getElementsByTagName('title')->item(0)->nodeValue;
 //I need to just store the link->href value to $link below
 //$link = ???;
}

Any suggestions on how to extract that "href" attribute?

Thanks!

like image 881
Rodney Blythe Avatar asked Dec 07 '10 17:12

Rodney Blythe


1 Answers

What about DOMElement::getAttribute?

$href = $node->getElementsByTagName('link')->item(0)->getAttribute('href');
like image 140
jwueller Avatar answered Oct 04 '22 20:10

jwueller