Jonah Hart Jonah Hart - 1 month ago 8
PHP Question

Can't Parse link from XML

public function load ( )
{

$xml = simplexml_load_file('https://www.google.co.uk/alerts/feeds/03124048891208938389/17727872730326737140');

foreach ( $xml -> entry as $entry )
{

$link = $entry -> link;

echo'<div class="row">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<div class="caption">
<h3>' . $entry -> title . '</h3>
<p>' . $entry -> content . '</p>
<p>' . $entry -> link . '</p>
</div>
</div>
</div>
</div>';

}

}

}


When I try to show "$entry -> link" to match the tag in the xml feed it doesn't show anything.

I don't know what the issue is, I've researched but to no avail.

I've tried assigning $entry -> link to a variable outside of the forreach but that wont crack it.

Answer

The <link>-tag itself is empty. What you try to access is an attribute.

<link href="example.com" />

You can get the attributes using the attributes()-function:

foreach ($xml->entry as $entry ) {
    //...
    echo '<p>'.$entry->link->attributes()->href.'</p>';
    //...
}

More information: https://secure.php.net/manual/en/simplexmlelement.attributes.php