Jump to content

Trouble with API call to pull invoices data


Recommended Posts

I am trying to follow the instructions on https://invisioncommunity.com/developers/rest-api for pulling in data from our forum.

Where last week I was able to print the data from the resulting $response, today the call for /nexus/invoices is returning "JSON_ERROR_SYNTAX" so nothing is being output.

$curl = curl_init( $url );
curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPAUTH		=> CURLAUTH_BASIC,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_USERPWD		=> "{$apiKey}:",
  CURLOPT_USERAGENT		=> "MyUserAgent/1.0"
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

$response = json_decode($response, true);

switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo ' - No errors';
        break;
        case JSON_ERROR_DEPTH:
            echo ' - Maximum stack depth exceeded';
        break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - Underflow or the modes mismatch';
        break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - Unexpected control character found';
        break;
        case JSON_ERROR_SYNTAX:
            echo ' - Syntax error, malformed JSON';
        break;
        case JSON_ERROR_UTF8:
            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
        default:
            echo ' - Unknown error';
        break;
    }

    echo PHP_EOL;
if ($response !== null) {
   echo $response->purchases->canceled;
} else {
   echo "Unknown";
}

This returns - Syntax error, malformed JSON Unknown

("Unknown" being from that last bit of the code)

Any help to kick me off here would be appreciated. I wasted a full day on this when I should have been formatting the resulting data into a useable report.

Link to comment
Share on other sites

11 hours ago, CoffeeCake said:

What changed?

As far as I know, nothing. I stopped working on it and had the array output printing on the page. I planned to come back and code the report the way I wanted it, but now I'm getting a 500 error.

 

Support keeps telling me they won't help with custom code so I'm trying to get eyes on it to see if it is my mistake as they suggest, but I haven't touched a thing so I don't see how the error is on my side. The API logs show 200 OKs on the call, but I keep getting JSON_ERROR_SYNTAX no matter what I do.

Link to comment
Share on other sites

I saw a character replacement snippet on Stack Overflow and tried that, but no luck. That code is below:

$curl = curl_init( $url );
curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPAUTH		=> CURLAUTH_BASIC,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_USERPWD		=> "{$apiKey}:",
  CURLOPT_USERAGENT		=> "MyUserAgent/1.0"
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

// Stack Overflow character fix snippet START
$checkLogin = $response;

// This will remove unwanted characters.
// Check http://www.php.net/chr for details
for ($i = 0; $i <= 31; ++$i) { 
    $checkLogin = str_replace(chr($i), "", $checkLogin); 
}
$checkLogin = str_replace(chr(127), "", $checkLogin);

// This is the most common part
// Some file begins with 'efbbbf' to mark the beginning of the file. (binary level)
// here we detect it and we remove it from the first 3 characters 
if (0 === strpos(bin2hex($checkLogin), 'efbbbf')) {
   $checkLogin = substr($checkLogin, 3);
}

$checkLogin = json_decode( $checkLogin );
print_r($checkLogin);
// Stack Overflow character fix snippet END
                     
$response = json_decode($checkLogin, true);

switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo ' - No errors';
        break;
        case JSON_ERROR_DEPTH:
            echo ' - Maximum stack depth exceeded';
        break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - Underflow or the modes mismatch';
        break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - Unexpected control character found';
        break;
        case JSON_ERROR_SYNTAX:
            echo ' - Syntax error, malformed JSON';
        break;
        case JSON_ERROR_UTF8:
            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
        default:
            echo ' - Unknown error';
        break;
    }

    echo PHP_EOL;
if ($response !== null) {
   echo $response->purchases->canceled;
} else {
   echo "Unknown";
}

 

Link to comment
Share on other sites

44 minutes ago, rekkajay said:

As far as I know, nothing.

Well, something did. It's just not obvious yet what changed. Perhaps the version of PHP, or the content of the outputted JSON, or something in your code, or the introduction of a CDN, or something else. 🙂

Have you examined the server logs to see what the server says is the reason for the 500 error?

Link to comment
Share on other sites

1 minute ago, CoffeeCake said:

Have you examined the server logs to see what the server says is the reason for the 500 error?

This.

 

2. Have you checked the response? What's the API returning? Is there anything suspicious in the response?

Link to comment
Share on other sites

The 500 error appears to be coming from the forum, not from my own server (it's a formatted 500 error block rather than the plain text Internal Server Error message my server displays), and nothing is logged in my server's access logs.

The API logs in my adminCP only show successful 200 OK connections, no failures.

/core/hello will output, but /nexus/invoices and /nexus/transactions won't. 

Link to comment
Share on other sites

In frustration, I went back to the original call using the sample in Invision's API documentation that I originally had, last week when /nexus/invoices was returning information.

$url = $communityUrl . "api" . $endpoint;

      $curl = curl_init( $url );
      curl_setopt_array( $curl, array(
          CURLOPT_RETURNTRANSFER	=> TRUE,
          CURLOPT_HTTPAUTH	=> CURLAUTH_BASIC,
          CURLOPT_USERPWD		=> "{$apiKey}:",
          CURLOPT_USERAGENT		=> "MyUserAgent/1.0"
      ) );
      $response = curl_exec( $curl );

print_r($response);

I went through the entire list of endpoints in the API documentation linked above, and the ONLY ones that return a 500 error are /nexus/invoices and /nexus/transactions.

/nexus/purchases DOES work, which in theory would contain the same strings as in invoices and transactions.

Does this help anyone pinpoint my issue? I'm banging my head against my desk, here, and I appreciate any help!

Link to comment
Share on other sites

I believe that's only for oAuth, correct? I'm using API Keys. According to the documentation, "using an API key, all data is available and all actions can be performed."

The API key was set to have all Endpoint permissions via System > Rest & OAuth.

Edited by rekkajay
more detail
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...