PHP Telegram bot gives empty variables from Google Maps API response

I’m developing a Telegram bot in PHP that’s supposed to access the Google Maps Distance Matrix API to get travel information. While the code runs correctly on testing platforms, it returns empty values when the bot is operational.

Here’s my code snippet:

$apiUrl = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=New+York&destinations=Brooklyn&mode=driving&sensor=false";
$jsonData = file_get_contents($apiUrl);
$dataArray = json_decode($jsonData, TRUE);
$travelDistance = $dataArray[rows][0][elements][0][distance][text];
$travelTime = $dataArray[rows][0][elements][0][duration][text];
$botMessage = "Travel time: $travelTime. Distance to cover: $travelDistance";

$botParams = array('chat_id' => $userId, "text" => $botMessage);
$botParams["method"] = "sendMessage";
echo json_encode($botParams);

The response from the bot is:

"Travel time: {. Distance to cover: {"

However, the API response appears correct:

{
   "destination_addresses" : [ "Brooklyn, NY, USA" ],
   "origin_addresses" : [ "New York, NY, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "8.2 mi",
                  "value" : 13201
               },
               "duration" : {
                  "text" : "28 mins",
                  "value" : 1680
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

Why are my variables returning empty fields? It seems like the JSON decode function is operating correctly, but accessing the array isn’t yielding the expected results.

Had the same problem with PHP bots and APIs. Besides the missing quotes thing others mentioned, you need to debug what’s actually coming back. Log the raw JSON response and the decoded array before you try to grab values. One thing I spotted - you’re missing an API key in your request URL. Google Maps has required auth for ages now, so without it you’ll get weird responses or hit rate limits. Just add &key=YOUR_API_KEY to your URL. Also throw in a quick check like if (isset($dataArray['rows'][0]['elements'][0]['distance']['text'])) before accessing values. APIs love changing their response structure, and this’ll stop your bot from crashing when they do.

Your array syntax is missing quotes around the keys. PHP needs string keys quoted when you’re accessing associative arrays.

Change this:

$travelDistance = $dataArray[rows][0][elements][0][distance][text];
$travelTime = $dataArray[rows][0][elements][0][duration][text];

To this:

$travelDistance = $dataArray['rows'][0]['elements'][0]['distance']['text'];
$travelTime = $dataArray['rows'][0]['elements'][0]['duration']['text'];

Without quotes, PHP thinks these are constants instead of string keys - that’s why you’re getting empty values. You should also add error checking for the API response and make sure you’re including your API key in the request URL since Google Maps API requires authentication.

You’re right about the quotes, but that’s just the tip of the iceberg. APIs fail, networks drop, rate limits hit - and you’re setting yourself up for a maintenance nightmare with direct API calls in your bot code.

I learned this the hard way building similar integrations. You need error handling, retry logic, response validation - it gets messy fast.

Skip wrestling with PHP arrays and API quirks. Set up the whole flow in Latenode instead:

  • Bot sends location request
  • Workflow calls Google Maps API (with proper error handling)
  • Response gets formatted cleanly
  • Data goes back to Telegram

The visual builder lets you see exactly what’s happening at each step. Built-in retry logic and error handling - no need for dozens of PHP lines.

I’ve done this for several bots. Way more reliable than cramming everything into bot code. Debugging’s cleaner too since you can pinpoint exactly where things break.