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

bryce alvord Posted in Component Development 9 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 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 = '';

    // set up cURL to make our call and return JSON
    $ch = curl_init($sheetsURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($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];
    return $balance;

And here is the code that works on PHP Tester

$json = '{
  "range": "Sheet1!A1:B40",
  "majorDimension": "ROWS",
  "values": [

$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 code and that doesnt work either.

us Bansh33 - Replied 9 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 9 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)) {