You can send custom user agent in PHP script using stream context and file_get_contents
function.
Here is an example:
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: My Custom User Agent Stringrn"
)
);
$context = stream_context_create($options);
// Replace with your URL
$response = file_get_contents('http://example.com', false, $context);
This example creates a GET request and sets a custom user agent string in the HTTP header. Change the URL to the one you want to send the request to. The server-side script handling the request will see your custom user agent string.
Please note, you must have allow_url_fopen
turned on in your php.ini
settings for the file_get_contents
function to work with URLs (it is on by default, but sometimes it is turned off for security reasons). If it's off and you don't want (or can't) to turn it on, you may want to use the curl extension instead, which also allows you to set custom headers including the user agent.
You can also use cURL in PHP to send a custom user agent. Here's how you can do it:
// Initialize cURL
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, 'http://example.com');
// Set the user agent
curl_setopt($ch, CURLOPT_USERAGENT, 'My Custom User Agent String');
// Will return the response, if false it prints the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the cURL request
$response = curl_exec($ch);
// Close cURL session
curl_close($ch);
In this example, you'll need to replace 'http://example.com'
with the URL you want to send the request to. The server-side script of the URL you're sending the request to will then see your custom user agent.