This topic is locked
[SOLVED]

 REST API Client Examples

1/18/2021 7:27:20 PM
PHPRunner General questions
S
salus1 authorDevClub member

Greetings,

Are there any complete PHP cURL examples available that demonstrate best practices for securely accessing SQL databases that have been enabled with REST APIs via PHP Runner's "REST API" button on the Miscellaneous panel?
I installed the latest 10.5 Enterprise build but the Help still references 10.4, was wondering if the 10.5 Help might include more detailed examples or if anyone can provide additional info.
Thanks!

T
Tim 1/19/2021

I don't have an answer to your question, but I can point you to the latest manual.
https://xlinesoft.com/phprunner/docs/index.htm

S
salus1 authorDevClub member 1/19/2021

Excellent, thank you very much.
So my question is how to alter the PHP provided in the manual (https://xlinesoft.com/phprunner/docs/rest-api-list.htm)...
<?php
$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_URL => "http://localhost:8086/api/v1.php?table=customers&action=list"'>http://localhost:8086/api/v1.php?table=customers&action=list";,

CURLOPT_RETURNTRANSFER => true,

CURLOPT_ENCODING => "",

CURLOPT_MAXREDIRS => 10,

CURLOPT_TIMEOUT => 0,

CURLOPT_FOLLOWLOCATION => true,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => "GET",

));

$response = curl_exec($curl);

curl_close($curl);

echo $response;

?>
...with the API key method from the manual(https://xlinesoft.com/phprunner/docs/rest-api-security.htm)...
curl -H "X-Auth-Token: dsagdsew45234etw435" "http://localhost:8086/api/v1.php?table=customers&action=list"'>http://localhost:8086/api/v1.php?table=customers&action=list";
A working example of the correct implementation of this stunning capability would blow people's minds.
It's like the last .05% of enabling a truly extraordinary feature.

Admin 1/19/2021

The manual can always use more examples. While this specific example is missing I will show two options of how this can be solved.

  1. You can Google PHP CURL examples that pass X-Auth-Token or just any other info via HTTP headers. Check the first answer here:
    https://stackoverflow.com/questions/13187462/sending-auth-in-headers-php-curl
  2. Also, you can mimic and test this kind of request using Postman or similar software and then Postman has "generate code" button that supports a number of languages including PHP.

S
salus1 authorDevClub member 1/19/2021

Thanks for that info. After some experimentation I found that the CURLOPT_HTTPHEADER had to be an array. The following now works like a charm (X-Auth-Token value altered below, need to supply your own).
Add the CURLOPT_HTTPHEADER => array("X-Auth-Token: oicu812.Y?") line to the View, List, Create, Update, Delete PHP examples in the Help file (https://xlinesoft.com/phprunner/docs/rest-api-list.htm) to enable this insanely cool feature.
I assumed that PHPRunner only read from APIs, not sure how many people know that it also provides the capability to generate APIs this quickly and easily. Definitely what I would classify as a killer feature.
Many thanks!

<?php

$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_HTTPHEADER => array("X-Auth-Token: oicu812y.Y?"),

CURLOPT_URL => "https://valucalc.com/pricegrid/api/v1.php?table=grid&action=list&records=10";,

CURLOPT_RETURNTRANSFER => true,

CURLOPT_ENCODING => "",

CURLOPT_MAXREDIRS => 10,

CURLOPT_TIMEOUT => 0,

CURLOPT_FOLLOWLOCATION => true,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => "GET",

));

$response = curl_exec($curl);

curl_close($curl);

echo $response;

?>
Admin 1/19/2021

Awesome, thank you for the update!