We all know that to get the content of a web page, we can use file_get_contents function. In this article, we want to set a header to this function.
We can use file_get_contents() as follows:
<?php $url = "http://example.com"; $content = file_get_contents($url); ?>
It's used in many web crawlers. But some servers, block their access to get their contents. One way to solve this issue is to use header in our codes. This means that, we can say to the server, our robot is a human being.
So, the server thinks mistakenly that our robot is a human and displays the main content.
<?php $options = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-language: en\r\n" . "Cookie: foo=bar\r\n" . "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) 20:23:10\r\n" ) ); $context = stream_context_create($options); $content = file_get_contents($url, false, $context); ?>