Skip to end of metadata
Go to start of metadata

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 = [
                "http"=> [ // it must be http, not https
                        "method"=>"GET",
                        "header"=> "User-Agent: {$http_user_agent}\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");