Search code examples
phpapigetipuser-agent

API : How to put useragent into url GET?


In php I would like to use API like this : website.com/api.php?ip=$ip&ua=$useragent.

However, with the useragent, variable $response return nothing (it displays nothing as if it was null).

I had tried with only IP address alone and it works fine.

My code :

$useragent = $_SERVER['HTTP_USER_AGENT'];
$ip = $_SERVER['REMOTE_ADDR'];
$query = 'https://website.com/api.php?ip='.$ip.'&page='.$useragent;
$response = file_get_contents($query);
echo $response;
?>

I think it's because of the characters in useragent, do you have any idea how to fix it? Thanks


Solution

  • Use the urlencode() function to encode special characters.

    And &page= should be &ua=.

    $query = 'https://website.com/api.php?ip='.$ip.'&ua='.urlencode($useragent);