Workaround in getting image details that returns a 403 error.

If you are trying to pull data that returns a 403 error then here’s the workaround for that. Use stream_context_create and use the result as a parameter in the file_get_contents.

In this example, it is trying to pull the image size of the image from the $url variable.

$ctx = stream_context_create(array(
        'http' => array('timeout' => 25, 'protocol_version' => 1.1,'user_agent' => 'My User Agent/1.0'),
        'https' => array('timeout' => 25, 'protocol_version' => 1.1,'user_agent' => 'My User Agent/1.0')
    ));

$dimension = getimagesizefromstring(  file_get_contents($url, 0, $ctx) );