3

I'm trying to use an API from avalara.net to get sales tax data and their API documentation lists what appears to be essentially a single line of code with a long string in JSON format for connecting to their API via cURL; I'm not familiar with this single string style and am trying to convert it into a format that follows the php manual's style, for example setting options via curl_setopt() instead of as inline commands like -u and -d but I'm not getting anywhere.

I already looked at php - implement curl command line to php, tried but getting error and added their solution - since I didn't get any error message I don't know if my problem was the same but it seemed like it wouldn't hurt to add CURLOPT_FOLLOWLOCATION. Didn't seem to make any difference.

I also looked at convert curl line command in php code but it didn't help as I already had curl_exec() in my code and had been reading the php manual pages.

Their code can be seen at https://github.com/avadev/AvaTax-Calc-REST-cURL/blob/master/tax-get-POST.txt and in relevant part is:

curl -u <AccountNumber>:<LicenseKey> -H "Content-Type: text/json" -d '{
--long string of data omitted; see link above for full data--
}' "https://development.avalara.net/1.0/tax/get"

I did some research and discovered that -u is for a username/password, -H is a special header, and -d is for the data to be sent... so this is what I put together:

// Identify the target URL
$url = 'https://development.avalara.net/1.0/tax/get';

// Start the process
$curl = curl_init($url);

// Tell cURL to fail if an error occurs
curl_setopt($curl, CURLOPT_FAILONERROR, 1);

// Allow for redirects; we don't know how avalara might route requests
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

// Assign the returned data to a variable
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// Set the timeout
curl_setopt($curl, CURLOPT_TIMEOUT, 5);

// Make sure to use POST method
curl_setopt($curl, CURLOPT_POST, 1);

// Set cURL to use a login:password for access
curl_setopt($curl, CURLOPT_USERPWD, "[1100033004]:[1FC8AED1543C699B]");

// Add some custom header info
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: text/json'));

// Use Heredoc syntax to encapsulate data, to avoid having to escape a million quotes
$data = <<<EOD
{
"DocDate": "2013-12-23",
"CustomerCode": "12345678123456781234567812345678",
"CompanyCode": "EGU",
"DocType": "SalesInvoice",
"Commit": false,
"Client": "Cool ERP,3,5",
"DocCode": "29",
"DetailLevel": "Tax",
"CustomerUsageType": "G",
"ExemptionNo": "12334",
"Discount": 0,
"PurchaseOrderNo":"PO 23423",                                        
"ReferenceCode":"",                                                        
"PosLaneCode":"",                                                                        
"BusinessIdentificationNo":"",
"TaxOverride":
{
"Reason":"Item Returned",
"TaxDate":"2013-05-05",
"TaxOverrideType":"TaxDate"
},
"Addresses":
[
{
"AddressCode": "Origin",
"Line1": "269",
"Line2": "7723 Tylers Place Blvd",
"City": "West Chester",
"Region": "OH",
"PostalCode": "45069-4684",
"Country": "US"
},
{
"AddressCode": "Dest",
"Line1": "1060 W. Addison St",
"City": "Chicago",
"Region": "IL",
"PostalCode": "60613-4566",
"Country": "US"
}
],
"Lines":
[
{
"LineNo": "00001",
"DestinationCode": "Dest",
"OriginCode": "Origin",
"ItemCode": "SP-001",
"Description": "Eyeglasses",
"TaxCode": "PC030147",
"Qty": 1,
"Amount": 100
}
]
}
EOD;
// That EOD ends the encapsulation via Heredoc syntax
// Note that the actual code does not indent the closing of Heredoc; only indented for stack overflow code view

// Set the POST data
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

// Execute the transaction
$r = curl_exec($curl);

// Close the connection
curl_close($curl);

// Print the results for debugging
print_r($r);

However, when I try to view the web page, I see nothing - no error message, no results, nothing.

I've looked at some of the other posts here on stackoverflow and in the php manual - I'm not sure at this point how to even debug it as I'm not getting any error messages back. Any thoughts would be appreciated.

Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
Eric Brockway
  • 101
  • 10
  • 1
    have you used the api in REST(addon in Mozilla)? you can debug there and get the response header and figure out what is going on ? – Kaushik Dec 23 '13 at 11:24
  • I didn't know there was such a addon - I'm going to add it immediately and start taking a look. – Eric Brockway Dec 23 '13 at 11:25
  • donot forget to add a custom header `Content-Type:application/json` Name is Content-Type and Value is application/json. – Kaushik Dec 23 '13 at 11:36
  • Interesting addon. I added a custom header and basic authentication - I got a few errors back from their server until I did that - but now I'm not seeing any response headers or body in that tool - none at all? Set to POST method, URL https://development.avalara.net/1.0/tax/get, Header is Content-Type: text/json, and Authentication set with the username/license key they provided via email.. and now no response at all. – Eric Brockway Dec 23 '13 at 11:39
  • you must getting some response header code check that out man – Kaushik Dec 23 '13 at 11:50
  • ok, I closed firefox and restarted it, then tested. With the URL, set to POST, and the custom header for text/json, IT GAVE THE EXPECTED OUTPUT. I had not added any authentication nor was I challenged... would it have saved that in the plugin from the previous attempts or does this point to an issue with the authorization? – Eric Brockway Dec 23 '13 at 12:15
  • Ok it may saved the settings like username and password you add at first time, so if you're getting the right result then go ahead. I dnt knw how to delete that username and password. but when you'll close the browser it will destroy automatically. – Kaushik Dec 23 '13 at 12:35

1 Answers1

2

This is a demo example to demonstrate the using of API in PHP this may help you to post some data to the API

$url = '<your url here>';
$curl = curl_init($url);

$data_string = '[
    {
        "FirstName": "Value"
    },
    {
        "EmailAddress": "Value"
    },
    {
        "Phone": "Value"
    }
]';

    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                                                'Content-Type:application/json',
                                                'Content-Length:'.strlen($data_string)

                                            ));

    $json_response = curl_exec($curl);
    $curl_errorno = curl_errno($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ($status) {
        echo 'Status is 200 OK! ';
    }else{
        echo "Sorry Something went wrong. Please retry!";
        curl_close($curl);
    }

figure out what you're getting in status code that will help you. EDIT

this will be the value of your

$data_string=
'{
    "DocDate": "2013-12-23",
    "CustomerCode": "12345678123456781234567812345678",
    "CompanyCode": "EGU",
    "DocType": "SalesInvoice",
    "Commit": false,
    "Client": "Cool ERP,3,5",
    "DocCode": "29",
    "DetailLevel": "Tax",
    "CustomerUsageType": "G",
    "ExemptionNo": "12334",
    "Discount": 0,
    "PurchaseOrderNo": "PO 23423",
    "ReferenceCode": "",
    "PosLaneCode": "",
    "BusinessIdentificationNo": "",
    "TaxOverride": {
        "Reason": "Item Returned",
        "TaxDate": "2013-05-05",
        "TaxOverrideType": "TaxDate"
    },
    "Addresses": [
        {
            "AddressCode": "Origin",
            "Line1": "269",
            "Line2": "7723 Tylers Place Blvd",
            "City": "West Chester",
            "Region": "OH",
            "PostalCode": "45069-4684",
            "Country": "US"
        },
        {
            "AddressCode": "Dest",
            "Line1": "1060 W. Addison St",
            "City": "Chicago",
            "Region": "IL",
            "PostalCode": "60613-4566",
            "Country": "US"
        }
    ],
    "Lines": [
        {
            "LineNo": "00001",
            "DestinationCode": "Dest",
            "OriginCode": "Origin",
            "ItemCode": "SP-001",
            "Description": "Eyeglasses",
            "TaxCode": "PC030147",
            "Qty": 1,
            "Amount": 100
        }
    ]
}'

ANSWER Remoe the braces form this line like this

curl_setopt($curl, CURLOPT_USERPWD, "1100033004:1FC8AED1543C699B");
  • Added curl_setopt($curl, CURLOPT_USERPWD, "[xxxxx]:[xxxxx]"); with provided credentials, still getting 400 status. – Eric Brockway Dec 23 '13 at 11:53
  • It means you're sending the wrong data. please use [jsonlint](http://jsonlint.com) to verify your json data –  Dec 23 '13 at 11:53
  • you can also add access key and password in url –  Dec 23 '13 at 11:57
  • jsonlint reported it as valid. – Eric Brockway Dec 23 '13 at 11:59
  • When you'll use REST with following details like Url : https://development.avalara.net/1.0/tax/get, `Content-Type:application/json`, method = `POST` then They are asking for some username and password after giving that what message you're getting ? please tell me the response header message. and don't forget to put the json data in body. –  Dec 23 '13 at 12:21
  • I was able to pull "{ "ResultCode": "Error", "Messages": [ { "Summary": "The user or account could not be authenticated.", "Severity": "Error", "Source": "Avalara.Web.Authorization"} ] } " with var_dump($json_response). I'm double-checking my credentials but I swear I copied and pasted them... – Eric Brockway Dec 23 '13 at 12:38
  • Confirmed credentials via copy/paste, plus I'm able to get expected result in REST addon for Firefox now... not sure why I'm getting 400 and 'could not be authenticated' with this code... – Eric Brockway Dec 23 '13 at 12:49
  • You can look at output that is clearly showing the error message that the credentials you've is not true. so communicate to someone from owner group so they can give you the right credentials. –  Dec 23 '13 at 12:50
  • Have you put some valid json data in body part of REST ?? with their required json format. Means array or single json element or it may be array inside an array etc . –  Dec 23 '13 at 12:52
  • I've been working in parallel with the other suggestion, using the REST addon for Firefox. With the same credentials, header setting of text/json, and copy/paste of json body after validating with jsonlint I'm getting the correct results in that debugging tool... but copy/paste that same info into my original code gives bool(false) and copy/paste into the code you provided (adding CURLOPT_USERPWD to handle credentials) and I get { "ResultCode": "Error", "Messages": [ { "Summary": "The user or account could not be authenticated.", "Severity": "Error", "Source": "Avalara.Web.Authorization"} ] } – Eric Brockway Dec 23 '13 at 13:06
  • Ok Sounds good Now Remoe the braces form this line like this `curl_setopt($curl, CURLOPT_USERPWD, "1100033004:1FC8AED1543C699B");` –  Dec 23 '13 at 13:16