简体   繁体   中英

Is it possible to perform a POST API call in PHP with authorization first?

I just started CS at University and wanted to create a little webdev project in my vacation. I created a simple contact form which asks the user for some data (text, dropdown choices). After pressing the submit button I would like to perform a POST api call to the UiPath Orchestrator to trigger a robot...

Since this is the very first time I'm dealing with PHP I'm not quite sure how to approach this problem. I'm able to grab the data from the submitted form. But now I'm struggling with the API call. I've tested it in Swagger UI and Postman and it works.

Problem: I have to authenticate via a batcher id which is also retrievable through an API call.

Question:

  1. Does it make sense to create a new function which is responsible for the API call, since I might want to add other forms which trigger other bot processes?

  2. From my research I'm quite sure that I have to use a cURL call (or is this wrong?).. If i need to authenticat every time the submit button is pressed, how can retrieve the token and pass it as a header (or handle idk) argument for the actual POST call?

If you cant understand my problem, I'm very sorry, I will try my best and reformulate it. but since I'm not experienced with this at all I hope you can forgive me.

Already looking forward for your help!

This is my code so far:

add_action('wpcf7_mail_sent','cf7_api_sender');

function cf7_api_sender($contact_form){ $title = $contact_form ->title;

if ($title === 'MA_Demo'){
    $submission = WPCF7_Submission::get_instance();

    if ($submission){
        $posted_data = $submission->get_posted_data();


        $firstname = $posted_data['Firstname'];
        $lastname = $posted_data['Lastname'];
        $department = $posted_data['Departement'][0];
        $workload = $posted_data['Workload'][0];


        /*

        this is what I found so far but ofc it does not work at all.. do I have to put the body (itemData) into the args? 
        and where can i pull the beacon token and add it?

        $url = '"https://cloud.uipath.com/..../..../orchestrator_/odata/Queues/UiPathODataSvc.AddQueueItem"
        $args =[
            accept: application/json
            X-UIPATH-OrganizationUnitId: .....
            {  \"itemData\": {    \"Name\": \"...\",    \"Priority\": \"...\",    \"Reference\": \"...\",    \"SpecificContent\": {\"key1\": \"Rick \", \"key2\": \"Roll\"}  }}
        ]

        */

        /* this was to test if it works to pull data 

        $myfile = fopen("data.txt", "w") or die("Unable to open file!");
        $txt = $firstname.$lastname.$department.$workload;
        fwrite($myfile, $txt);
        fclose($myfile);

        */
    }
}

}

First you'll need to make a call to /api/Account/Authenticate with your tenant, username, and password. It will return something like

{
  "result": "HzptFsZpGMS64j5DTb4TqX-cHVv2AtC4noVCQrkHKr54r...",
  "targetUrl": null,
  "success": true,
  "error": null,
  "unAuthorizedRequest": false,
  "__abp": true
}

Then you use that result in your Add Queue Item call by adding a header item

"Authorization":"Bearer " & result  ("Bearer HzptFsZpGMS64...")

This is for Orchestrator on-prem 2019.10 so cloud may be a little different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM