Php rest api

Good morning.

Does anyone have a PHP environment they use with REST? I’m really struggling to find one or make mine work so I wondered if anyone would mind sharing their source files.

Kind regards,
Aaron.

Share some code and error messages to see where the struggle is.

Can you successfully make REST calls in Postman or Curl?

Or is this more of a generic PHP question?

Hi Mark,

I can successfully make calls in Postman but it’s transforming that to PHP

Kind regards,
Aaron.

Care to post some code you’ve tried? When posting code on the site, use three back-ticks on a line above and below for better formatting.

<?php
echo 'Hello world'
?>

Also, you can make Postman work with a bad certificate. If you run the call in the browser, do you get a secure connection? You cannot do REST without a secure connection - especially if you’re using Basic authentication where your password is only Base64 encoded.

http://docs.guzzlephp.org/en/stable/

Also you can read

3 Likes

I wrote a small framework for epicor rest in php just a helper library based off our .net one. It’s not super rounded out only built what I needed as it’s a middle ground solution, but still might help you get started.
EpicorRESTLib.php.txt (8.7 KB)

usage would be similar to below

protected function InvoiceLookup($message = "")
    {
        //Instanciate
        $epicorREST = new EpicorRESTLib(WEI_Options::getOption("HostUrl"), WEI_Options::getOption("EpicorInstance"), WEI_Options::getOption("EpicorUsername"), WEI_Options::getOption("EpicorPassword"));
        //Call Settings allow Plant, Company and such this can be passed to any call as an optoinal param
        $callSettings = new CallSettings(WEI_Options::getOption("EpicorCompany"),"","",WEI_Options::getOption("EpicorPlant"));
        
        $baqParams['pCustID'] = $_POST['epicor_custid'];
        $baqParams['pInvoiceNum'] = $_POST['epicor_invoicenum'];

        $aged = json_decode($epicorREST->GetBAQResults("WP-PLUGIN-ARAging", $baqParams, $callSettings))->value;
        
        $baqParams['$top'] = 25;
        $baqParams['$skip'] = 0;
        $data = json_decode($epicorREST->GetBAQResults("WP-PLUGIN-InvoiceStatus", $baqParams, $callSettings))->value;

        if(count($aged) > 0 || count($data) > 0)
        {
            include_once WEI_ABSPATH . "includes/templates/wei-template-shortcode-invoice-status.php";
        }
        else
        {
            return $this->LookupForm("Unable to obtain invoice status.<br />".json_decode($epicorREST->ErrorMessage())->ErrorMessage);
        }
    }
6 Likes

Thank you that’s very helpful.

I’m new to all of this Epicor API stuff… like you say it’s something to get me started. I can’t even make a connection to the API and display the text in a nice format yet…

I’m sure i’ll get there!

Thanks again.

1 Like

Just noticed this thread.
I’ve been using PHP for REST for some time. Initially I wrote all my calls using CURL but that started to get a bit tedious. I looked around for a library and didn’t find anything I liked. Then I stumbled on OpenAPI Generator. https://openapi-generator.tech/ or GitHub - OpenAPITools/openapi-generator: OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3).

This tool will generate library code for whatever services you’re interested in using. It can generate in many different languages of course I was only interested in PHP.

2 Likes

There is also Microsoft’s open source tool:

GitHub - Azure/autorest: OpenAPI (f.k.a Swagger) Specification code generator. Supports C#, PowerShell, Go, Java, Node.js, TypeScript, Python, Ruby

Updated the EpicorRestLib for PHP to support epicor rest v1 and v2
EpicorRESTLib.php (2).txt (11.0 KB)

3 Likes