You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

To put custom User-Agent value with file_get_contents, you will need to create a stream_content.

Below example, show how to make a call with custom HTTP_USER_AGENT

// custom_file_get_contents by Chun Kang (ck@ckii.com)

function custom_file_get_contents( $url, $custom_http_user_agent="Mozilla/5.0 (CentOS; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 (ck)")
{
        // define options
        $options = array(
                'http'=>array( // it must be http, not https
                  'method'=>"GET",
                  'header'=>
                        "User-Agent: {$custom_http_user_agent}\r\n" .
                        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\n" .
                        "Accept-Language: en-US\r\n" .
                        "Connection: keep-alive\r\n"
                )
        );
              
        $context = stream_context_create($options);
        $buff=@file_get_contents( $url, false, $context);
        return $buff;
}

echo custom_file_get_contents( "http://yahoo.com");
echo "\n\n"
echo custom_file_get_contents( "https://yahoo.com");
  • No labels