Search code examples
phpfile-get-contentsmeta

Using file_get_contents to get og:image meta data throws "failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden" with some websites


I'm trying to use php to get meta data like og:image, title or description.

I'm using that code:

<?php
$sites_html = file_get_contents($url);

$html = new DOMDocument();
@$html->loadHTML($sites_html);
$meta_og_img = null;
//Get all meta tags and loop through them.
foreach($html->getElementsByTagName('meta') as $meta) {
    //If the property attribute of the meta tag is og:image
    if($meta->getAttribute('property')=='og:image'){
        //Assign the value from content attribute to $meta_og_img
        $meta_og_img = $meta->getAttribute('content');
    }
}
echo $meta_og_img;
?>

When I use this url (https://www.elmundo.es/papel/2019/01/28/5c4ed8effc6c83d2718b4605.html) it works perfectly but when I use this one (https://andresmartin.org/2016/09/mindfulness-la-fibromialgia-mirar-dolor-amabilidad-alivia-malestar-reduce-dolor/), I get the error.

How can I avoid this error? And if it is impossible to do, how can I get the meta data with another method?

I think it is not important but I'm using laravel.

EDIT: Here is a screenshot of the error https://pasteboard.co/HYPI7KV.png


Solution

  • Finally I found the way.

    I added:

    $context = stream_context_create(
        array(
            "http" => array(
            "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
            )
        )
    );
    $sites_html = file_get_contents('https://andresmartin.org/2016/09/mindfulness-la-fibromialgia-mirar-dolor-amabilidad-alivia-malestar-reduce-dolor/', false, $context);
    

    Now it works fine.