I am using this script to get all images from a generic external webpage:
$url = ANY URL HERE;
$html = @file_get_contents($url,false,$context);
$dom = new domDocument;
@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
echo $image->getAttribute('src');
}
But in some cases like this ( where the image is in "rel:image_src" )
<img src="http://example.com/example.png" rel:image_src="http://example.com/dir/me.jpg" />
it doesn't work.
How can I do ?
Check if the node has an attribute rel:image_src
foreach ($images as $image) {
if( $image->hasAttribute('rel:image_src') ) {
echo $image->getAttribute('rel:image_src');
} else {
echo $image->getAttribute('src');
}
}
you could include both:
foreach ($images as $image) {
echo $image->getAttribute('src');
echo $image->getAttribute('rel:image_src');
}
If you want the rel:image_src
to take precidence, check for the attribute's presence and use it selectively:
$url = ANY URL HERE;
$html = @file_get_contents($url,false,$context);
$dom = new domDocument;
@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
if ($image->hasAttribute('rel:image_src')
{
echo $image->getAttribute('rel:image_src');
}
else
{
echo $image->getAttribute('src');
}
}