REST API: How to process Results from /amember/api/invoices

Discussion in 'Customization & add-ons' started by wikaeaaccount, Oct 17, 2014.

  1. wikaeaaccount

    wikaeaaccount aMember Pro Customer

    Joined:
    Jan 15, 2014
    Messages:
    9
    I have made an attempt, which I will share here, but it is very basic. All I really need is the invoice_id and public_id, so I am going to cut corners ;-P

    First I had to add this option to the example on the wiki to get the result to return from the curl_exec.
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);

    After that I could add this:
    $resultcurl = curl_exec($ch);

    I am including this in a function, which I could then set to:
    return $curlresult;

    This information is then processed with a really short code:

    PHP:
    $array explode("{",$resultcurl);
    $array explode(",",$array[1]);
    foreach (
    $array as $var) {
        
    $split explode(":",$var);
        
    $result[str_replace("\"","",$split[0])] = str_replace("\"","",$split[1]);
    }
    echo 
    "invoice_id: ".$result["invoice_id"]."<br>";
    echo 
    "public_id: ".$result["public_id"]."<br>";
    As you see, I am just grabbing the first section that seems to always contain the invoice_id and public_id. I then save this to the database with the $resultcurl. If there is an error, I can also capture that when I have CURLOPT_RETURNTRANSFER activated and save that to the db as well.

    Once I was finished, I started thinking that maybe someone here could find this useful, so here is my very basic solution... If you have something better, please let me know, but at least this might help someone out there.

    So, did you? If anyone found this useful, let me know :)
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    I am sorry but I think it is bad advice. aMember returns data in well know data-interchange formats such as json, xml or serialize on your chose.

    You can use build in PHP functions to decode it:
    json: http://php.net/json_decode
    xml: http://php.net/simple_xml
    serialize: http://php.net/unserialize

    You can choose format of result data by submitting additional parameter _format in API query. It can be json (default), xml or serialize.
  3. wikaeaaccount

    wikaeaaccount aMember Pro Customer

    Joined:
    Jan 15, 2014
    Messages:
    9
    Ah, I did not know that. This is great! So then, I guess I am better off using this format:

    Code:
    $jsondata = json_decode($resultcurl, true);
    echo "invoice number: ".$jsondata[0]["invoice_id"]."<br>";
    echo "invoice number: ".$jsondata[0]["public_id"]."<br>";
    This produces the desired results for my examples. The invoice_id and public_id are always contained in the array within [0]?

    I still need to use the CURLOPT_RETURNTRANSFER to get the value returned to $resultcurl, right? Perhaps the data is somehow added to a variable, but I just dont know about it?
  4. wikaeaaccount

    wikaeaaccount aMember Pro Customer

    Joined:
    Jan 15, 2014
    Messages:
    9
    How do you know if the query failed?

    I tried using the wrong key and this produced this error:
    Code:
    {"ok":false,"error":true,"message":"API Error 10002 - [key] is not found or disabled"}
    What is a safe way to know if the script failed?

    Code:
    if ($jsondata["error"]) { echo "ERROR!<br>"; }
    else { echo "run script here"; }
    Does that work for all your error messages?
  5. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Yes, it works for all error messages.

Share This Page