php get main image of webpage

Solutions on MaxInterview for php get main image of webpage by the best coders in the world

showing results for - "php get main image of webpage"
Till
29 Apr 2020
1<?php
2$html = file_get_contents('http://www.website.any');
3preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i',$html, $matches ); 
4echo $matches[ 1 ][ 0 ];
5
6//or
7 
8libxml_use_internal_errors(true); // Yeah if you are so worried about using @ with warnings
9$doc = new DomDocument();
10$doc->loadHTML($html);
11$xpath = new DOMXPath($doc);
12$query = '//*/meta[starts-with(@property, \'og:\')]';
13$metas = $xpath->query($query);
14$rmetas = array();
15foreach ($metas as $meta) {
16    $property = $meta->getAttribute('property');
17    $content = $meta->getAttribute('content');
18    $rmetas[$property] = $content;
19}
20var_dump($rmetas);
21
22// or    Using https://github.com/scottmac/opengraph
23$graph = OpenGraph::fetch('http://www.avessotv.com.br/bastidores-pantene-institute-experience-pg.html');
24print_r($graph);
25
26?>
27    
28