The Mobile Unicom Base Station API is a service provided by Juhe Data, mainly used to query the base station information corresponding to a phone number. It includes details such as the province and city location, base station code, and base station name. With this API, developers can easily obtain geographic and network operator information related to phone numbers.
Below is a complete PHP example for calling the Mobile Unicom Base Station API and parsing the returned JSON data:
$appkey = "your_appkey"; // Replace with your own AppKey
$mobile = "your_mobile"; // Replace with the phone number you want to query
$url = "http://apis.juhe.cn/mobile/get?dtype=json&phone={$mobile}&key={$appkey}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
curl_close($ch);
$result = json_decode($res, true);
if ($result) {
if ($result['error_code'] == 0) {
$data = $result['result'];
echo "Phone Number: {$data['phone']}";
echo "Province: {$data['province']}";
echo "City: {$data['city']}";
echo "Operator: {$data['company']}";
echo "Area Code: {$data['areacode']}";
echo "Base Station Code: {$data['cellcode']}";
echo "Base Station Name: {$data['cellname']}";
} else {
echo "Query failed: " . $result['reason'];
}
} else {
echo "Request failed";
}
?>
Before using, replace the AppKey with your valid key obtained from Juhe Data, and replace the phone number with the one you want to query. The code constructs the request URL by appending the phone number and AppKey as parameters.
This example uses the curl library to perform the API call, setting the option to return the response as a string. After executing the request, the connection is closed. Then, the JSON response is decoded into a PHP array for easy data handling.
The code checks the API return status; if successful (error_code equals 0), it retrieves and outputs the base station related information from the result field. If it fails, the error reason is displayed for troubleshooting.
The PHP code example provided in this article helps developers quickly integrate mobile Unicom base station querying functionality to easily obtain base station information linked to phone numbers, which is useful for network optimization, geolocation, and data analysis.
In practical use, please control the API call frequency reasonably to avoid service restrictions due to excessive requests. Also, never expose your AppKey to ensure account security.
Mastering curl requests and JSON parsing are common and useful skills in PHP development. This example will further enhance your ability to apply these technologies.