"Hotlinking is forbidden" when using file_get_contents () [closed]

3

This error appears when I try to use the file_get_contents() function of PHP:

  

failed to open stream: HTTP request failed! HTTP / 1.1 403 Hotlinking is forbidden

As a parameter we have a normal PHP page, which makes some connections to the client database and an internal one. This target does not have header at all. Depending on what goes on inside (it's all right), it will "start" (with echo) a JSON. And this is what I want to get for my file_get_contents . My hotlink is disabled.

What can I do to fix it?

    
asked by anonymous 20.09.2016 / 05:24

1 answer

5

Normally hotlinks are detected by header Referer sent by browser .

To specify a header in the use of file_get_contents , we have stream_context_create :

$opts = array(
   'http'=>array(
      'header'=>'Referer: http://www.enderecodositerequisitado.com'."\r\n"
   )
);

$context = stream_context_create($opts);

$file = file_get_contents(
    'http://www.enderecodositerequisitado.com/caminho', false, $context
);

In this way, the behavior is similar to an access made by a link of the site itself. Note that protection may be more complex than this, but in most cases, the solution is this way.

In the example above, the important thing is that Referer has origin as something expected in normal use of the site by a user who is browsing normally.

Alternatively, instead of doing all this, it could be the case to simply put the IP of your server on a white list on the server on the side that provides the data.

    
20.09.2016 / 13:17