This post is totally off topic, but I can’t figure out how to post code as a comment on Sergey’s blog post about working with the REST APIs in Acumatica (click here).
So, I’m posting the code here, then I’ll refer to this post in my comment on Sergey’s blog post.
But, since I’m posting it here, I guess I need to do a little more explaining.
I’m currently working on an integration that is going to make an ODBC call and push the results into Acumatica via the Acumatica REST APIs.
DISCLAIMER: Real developers and programmers will probably critique my method and find all kinds of things wrong with it, but I’m more of a “duct tape” programmer. If I can find something that works, then I’m going to try it. I don’t really care if it takes 5.643 milliseconds longer to run than a better method or if the method I used won’t hold up under the academic scrutiny of a PHD computer science research paper. Just make it work, then, if I need it to work BETTER, I’ll worry about that if (and when) I ever arrive at that problem. The IF is key here because most of the time (in my opinion), the “duct tape” holds just fine and is still in use 10 years from now. That’s why so many spreadsheets last for decades before they get replaced. Ok, that’s my disclaimer.
I asked myself what would be the quickest way to code this integration. I don’t know C# and I don’t want to spin up a Visual Studio environment on my computer. I’ve done that before and it made my computer run really slow even though I tried to uninstall everything when I was done.
I just needed a simple scripting language that could make ODBC calls and work with the Acumatica REST APIs.
That’s when I remembered that I used to use PHP way back in college when I worked on an open source project at UCLA. We used Linux, PHP, Perl, and MySQL to in-house code something we called ClassWeb and used it to run all of the Social Science class sites at UCLA.
It’s been a while, but hey, I thought I would try to dust off some of that PHP knowledge.
First, I had to get PHP working on my local IIS Windows 10 environment. Check.
Second, I had to get the ODBC calls working. Check.
Third, I had to try a test Acumatica REST API call. This one I’ve already done in Postman before thanks to these great sessions (click here, and click here). Check.
Fourth, I had to translate what I did in Postman into PHP.
This is where things got tricky.
There is a cool Code button in Postman that allows you to take the work that you’ve done in Postman and translate it into another language.
I decided to go with the PHP cURL option, so I selected it in the screenshot below.
Then I ran the code, but I got the following error:
{“message”:”You are not logged in.”}
Hmmm, what could be wrong? Let’s think about what is happening here.
There are basically three steps in my Postman logic:
- Login
- Create a Customer
- Logout
I could see that my error message was happening at the second step and the problem had to do with the first step not passing the session information to the second step. That’s why I was getting the “you are not logged in” error.
A little investigating (this is is when I landed on Sergey’s post), and it seemed that I needed a way to pass the cookies generated from the first step to the second step.
Some more investigating and I think I finally found the way to do it in PHP.
So here is the code. I ran it in my local environment and it seems to be working. Let’s see how long the duct tape holds 🙂
<?php
// Add Cookie Jar
$cookie_jar = tempnam(‘/tmp’,’cookie’);
// Initiate Connection
$curl = curl_init();
// Login to Acumatica REST API
curl_setopt_array($curl, array(
CURLOPT_URL => “http://localhost/Acumatica/entity/auth/login”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_COOKIESESSION => 1,
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “POST”,
CURLOPT_POSTFIELDS => “{\r\n  \”name\”: \”admin\”,\r\n  \”password\”: \”mypassword\”,\r\n  \”company\”: \”Company\”\r\n}”,
CURLOPT_HTTPHEADER => array( “cache-control: no-cache”, “content-type: application/json”, “postman-token: e0a0ff40-8d46-4c5f-106b-960ad1aafba8”
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo “cURL Error #:” . $err;
} else {
echo $response;
}
// Create Sample Customer With Acumatica REST API
curl_setopt_array($curl, array(
CURLOPT_URL => “http://localhost/Acumatica/entity/Default/6.00.001/Customer”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_COOKIESESSION => 1,
CURLOPT_COOKIEFILE => $cookie_jar,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “PUT”,
CURLOPT_POSTFIELDS => “{\r\n \”CustomerID\” : {value : \”NewOne\” } ,\r\n \”CustomerName\” : {value : \”It Worked\” },\r\n}”,
CURLOPT_HTTPHEADER => array( “cache-control: no-cache”, “content-type: application/json”, “postman-token: 5248821b-91e9-5800-bd9c-b4c9775c6c5a”
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo “cURL Error #:” . $err;
} else {
echo $response;
}
// Logout of Acumatica REST API
curl_setopt_array($curl, array(
CURLOPT_URL => “http://localhost/Acumatica/entity/auth/logout”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_COOKIESESSION => 1,
CURLOPT_COOKIEFILE => $cookie_jar,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “POST”,
CURLOPT_HTTPHEADER => array( “cache-control: no-cache”, “content-type: application/json”, “postman-token: e4363994-cc86-330c-17d5-0bc9926c6fc2”
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo “cURL Error #:” . $err;
} else {
echo $response;
}
// Close Connection
curl_close($curl);
// Remove Cookie Jar
unlink($cookie_jar) or die(“Can’t unlink $cookie_jar”);
?>