Decoded JSON response from GoogleSheetAPI v4 is not working in a WIP component

bryce alvord Posted in Component Development 5 months ago

I have run into a roadblock that I cant figure out that Im sure is something simple. The code I have written works on phptester.net but does not return anything once in my component project.

Here is the actual code from the component

/**
 * billing_user_balance
 * summary = Call the google sheets API and get a JSON object back of accounts
 *           and their balances to report to thr front end the balance of the 
 *           logged in user.
 * param $last = last_name of logged in user
 * param $first = first_name of logged in user
 * returns $balance = balance for that user from API call
*/
function billing_user_balance($last = "", $first = "") {
    // initialize working variables
    $balance = "*ERROR* Contact Admin";
    $sheetsURL = "https://sheets.googleapis.com/v4/spreadsheets/1bH6LmIsDQQdd1Beiq1s9fI-SuYRoGYmBc-_IYrpux_g/basic/A1%3AB40?key=key";

    // set up cURL to make our call and return JSON
    $ch = curl_init($sheetsURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    // get JSON object into arrays that we can work with
    $accounts = json_decode($repsonse, true);
    $accounts = $accounts["values"];

    // loop through unnamed arrays to find the array we care about
    // once found, we will set $balance to the balance which is [1]
    foreach($accounts as $account) {
        if ($account[0] == "$last,$first") {
            $balance = $account[1];
            break;
        }
    }
    return $balance;
}

And here is the code that works on PHP Tester

<?php
$json = "{
  "range": "Sheet1!A1:B40",
  "majorDimension": "ROWS",
  "values": [
    [
      "Name",
      "Balance"
    ],
    [
      "Allred,Steve",
      "$100.00"
    ],
    [
      "Kuchner,John",
      "$400.00"
    ],
    [
      "Smith,Alex",
      "$300.00"
    ],
    [
      "Thomas,Jane",
      "$750.00"
    ],
    [
      "Williams,Sally",
      "$600.00"
    ]
  ]
}";

$accounts = json_decode($json, true);
$accounts = $accounts["values"];
foreach($accounts as $account) {
    if ($account[0] == "Smith,Alex") {
        echo $account[1];
    }
}

I have tried hard coding the response like I did in the phptester.net code and that doesnt work either.

Replies
us Bansh33 - Replied 5 months ago

Looks like you've figured out the issue and are good to go, but I just thought I should mention that by default, ossn will terminate at the point of any errors, which can make it kinda default to debug if you aren't aware of this. You can change the error reporting in the admin console, and then you should be able to read the error_log in your site's root. Just thought that might be helpful to know for the future.

us Bryce alvord Replied 5 months ago

I figured it out and it was just me being dumb.

Changed this

if ($account[0] == "$last,$first") {

To this

if ($account[0] == trim($last) . "," . trim($first)) {