AUGForums.com

An Acumatica User Group

  • Forums
  • Podcast
  • Blog
  • Rolodex
  • Login
  • Start Here
  • Courses
  • Register

Acumatica REST API and PHP Curl

September 19, 2017 by Tim Rodman

I wanted a way to push data into an Acumatica REST API using PHP Curl. The challenge was staying logged in. It took some research, but this is how I did it.

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.

Acumatica REST API and PHP Curl

 

I decided to go with the PHP cURL option, so I selected it in the screenshot below.

Acumatica REST API and PHP Curl

 

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:

  1. Login
  2. Create a Customer
  3. 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”);
?>

Filed Under: Miscellaneous Tagged With: Acumatica, Acumatica Blog, Acumatica Integrations, Acumatica Learning, Acumatica REST API, Acumatica Training, PHP, PHP Curl

By using this website, you agree to our Terms of Use (click here)

Online Members

Recent Blog Posts

  • EP 37: Jody Lorincz, IT Manager at MHR Brands, sharing his Acumatica Customer Story (Podcast) January 13, 2021
  • EP 36: What Motivates ERP Veteran Nicole Ronchetti To Participate in Acu-Connect (Podcast) January 8, 2021
  • Acumatica and Scanco WMS – A Cautionary Tale for ISVs January 6, 2021
  • EP 35: Ideas for AUGForums.com Live with Ryan Brown (Podcast) December 12, 2020
  • EP 34: Talking Acumatica eCommerce and the Next Normal for Distribution, Wholesale, and Manufacturing with Ted Stenstrom (Podcast) December 11, 2020
Acumatica Learning Resources

Recent Forum Posts

  • RE: Generic Inquiry RESULTS GRID Column Order

    On the Results Grid tab you can drag and drop the items...

    By Michael Roszkowski, 4 mins ago

  • RE: Acumatica 2020 R2 OwnerID Field

    Thank you, Tim. Just faced this problem in a customized...

    By Anna Borisova, 15 hours ago

  • Import Azure AD Groups to User Roles Screen

    Hi AUG, I'm cross posting this from the community as I ...

    By Michael.Barker, 16 hours ago

  • Link EPApproval to APInvoice

    I am trying to write a GI that will link an Invoice to ...

    By Michael Roszkowski, 17 hours ago

  • RE: Using SMS

    Lightspeed Voice has a message service "built" in. Thi...

    By Doug, 22 hours ago

  • User access right - cost on Sales Order

    Hi, I have a problem that i can't solve. I have build...

    By Fosse, 1 day ago

  • RE: Problem when i have more then one Alternate ID

    Thanks for the answers! Unfortunately I don't think ...

    By Fosse, 1 day ago

  • RE: Using « start with » unstead of « contrains »

    It's a contains search, not a "starts with" search unfo...

    By Tim Rodman, 1 day ago

  • RE: Problem when i have more then one Alternate ID

    This article I wrote explains how to work with barcodes...

    By Carl Brooks, 2 days ago

Recent Tweets

Terms of Use & Disclaimers :: Privacy Policy

Copyright © 2021 · AUG Forums, LLC. All rights reserved. This website is not owned, affiliated with, or endorsed by Acumatica, Inc.