Current Location: Home> Latest Articles> Use get_client_version in API to identify the calling device type

Use get_client_version in API to identify the calling device type

gitbox 2025-05-11

When developing a Web-based API service, understanding the type of client devices is of great significance to optimizing response content, log analysis, security control, etc. Through the get_client_version function, we can effectively identify the device type from which the request comes from, such as mobile phones, tablets, desktop devices, etc. This article will introduce how to implement this feature in PHP and help you better understand and apply it through practical examples.

1. What is the get_client_version function?

get_client_version is a custom function (or similar functions already exist in some frameworks or libraries) that parses user-agent strings (User-Agents) to determine the client's operating system, browser, and device type. Usually this function will combine regular expressions or third-party libraries to achieve recognition functions.

2. Obtain User-Agent information

First, we need to get User-Agent information from the request header:

 $userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';

This is the information carried by the browser or App when sending a request. By analyzing the contents in it, we can judge the device type.

3. Implement the get_client_version function

Here is a simple example of the get_client_version implementation that identifies three main device types: mobile (mobile), tablet (tablet), and desktop (desktop).

 function get_client_version($userAgent) {
    $deviceType = 'desktop'; // Default Type

    if (preg_match('/mobile|iphone|android/i', $userAgent)) {
        $deviceType = 'mobile';
    } elseif (preg_match('/ipad|tablet/i', $userAgent)) {
        $deviceType = 'tablet';
    }

    return $deviceType;
}

You can further expand this function according to your needs, such as adding operating system recognition, browser version, brand recognition, etc.

4. Apply device recognition logic in API

Suppose we have a simple API interface that returns the client device type. Here is a complete code example:

 header('Content-Type: application/json');

$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$deviceType = get_client_version($userAgent);

$response = [
    'status' => 'success',
    'device_type' => $deviceType,
    'message' => "What are you using{$deviceType}equipment"
];

echo json_encode($response);

When the client accesses the interface (for example via https://api.gitbox.net/detect-device ), the API returns a JSON response with the following structure:

 {
  "status": "success",
  "device_type": "mobile",
  "message": "What are you usingmobileequipment"
}

5. Test suggestions

You can use the following methods to test the recognition accuracy of this API:

  • Access using different devices (mobile phone, tablet, computer)

  • Modify the browser's User-Agent

  • Send requests using Postman or cURL and set different User-Agent headers

The example cURL command is as follows:

 curl -H "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X)" https://api.gitbox.net/detect-device

6. Conclusion

Through the get_client_version function combined with the User-Agent header information, we can effectively identify the client's device type in the API developed by PHP. This mechanism is not only suitable for customized response content, but also for security control and access statistics. In actual projects, it is recommended to further modularize this function and combine it with a third-party device identification library to improve the recognition accuracy.

I hope this article will be helpful to your device recognition function implementation in PHP API development.