Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active September 18, 2024 04:18
Show Gist options
  • Save vyspiansky/82f4b1ef6fcff160047d to your computer and use it in GitHub Desktop.
Save vyspiansky/82f4b1ef6fcff160047d to your computer and use it in GitHub Desktop.
PHP: set user agent using file_get_contents()
<?php
$options = array('http' => array('user_agent' => 'custom user agent string'));
$context = stream_context_create($options);
$response = file_get_contents('http://domain/path/to/uri', false, $context);
@mikenk2010
Copy link

Works like a charm!!! 🥰

@burkybang
Copy link

This was helpful to me. Thanks!

@hgc81538
Copy link

Or set it globally

ini_set('user_agent', 'SomeBrowser v42.0.4711');

$response = file_get_contents('http://domain/path/to/uri');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment