Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.

Moderators: ArturoRobles, ArturoRobles

Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
User avatar
By RicardoMG
#827841
Hello!
At the moment processmaker can not return all cases, but you can review what you can do REST API Cases - cases in this link: https://wiki.processmaker.com/3.3/REST_API_Cases/Cases
An example similar to what you were asking was made.
First I got the token using Postman, see the attached images.
You must take into account that to obtain the token you first have to Registering an External Application to Use REST, you have to follow this steps: https://wiki.processmaker.com/3.1/OAuth_2.0

Then you will have the client_id and client_secret, use that data with a user registered in processmaker to obtain the token.

Now i am going to perform using a trigger to get the number of cases started in processmaker, this is the code in the trigger:
Code: Select all
$pmRestRequest = function ($method, $pmServer, $endpoint, $aVars = null, $accessToken = null) {
        echo ($pmServer.$endpoint);
        if (empty($accessToken) and isset($_COOKIE['access_token']))
        $accessToken = $_COOKIE['access_token'];
    if (empty($accessToken)) { //if the access token has expired
        //To check if the PM login session has expired: !isset($_COOKIE['PHPSESSID'])
        header("Location: ./../login/login"); //change to match your login method
        die();
    }
    //add beginning / to endpoint if it doesn't exist:
    if (!empty($endpoint) and $endpoint[0] != "/")
        $endpoint = "/" . $endpoint;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $pmServer . $endpoint);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $accessToken));
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $method = strtoupper($method);
    switch ($method) {
        case "GET":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
            break;
        case "DELETE":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            break;
        case "PUT":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        case "POST":
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($aVars));
            break;
        default:
            throw new Exception("Error: Invalid HTTP method '$method' $endpoint");
            return null;
    }
    $ret = curl_exec($ch);
    $oRet = new StdClass;
    $oRet->response = json_decode($ret);
    $oRet->status   = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($oRet->status == 401) { //if session has expired or bad login:
        header("Location: ./../login/login"); //change to match your login method
        die();
    }
    elseif ($oRet->status != 200 and $oRet->status != 201) { //if error
        if ($oRet->response and isset($oRet->response->error)) {
            print "Error in $pmServer:\nCode: {$oRet->response->error->code}\n" .
                "Message: {$oRet->response->error->message}\n";
        }
        else {
            print "Error: HTTP status code: $oRet->status\n";
        }
    }
    return $oRet;
};
$cases=1;
$token="7ee4ff6dcadd345b07349e92c12e087bb849d27d";
$oRet = $pmRestRequest('GET', "http://192.168.0.109:8080",'/api/1.0/workflow/case/start-cases', null, $token);
if ($oRet and $oRet->status == 200) {
    foreach ($oRet->response as $oUser) {
		$cases = $cases + 1;
        print "{$oUser->usr_firstname} {$oUser->usr_lastname} ({$oUser->usr_username})\n";
    }
	@@number = $cases;
}
In the variable @@number obtaining the number of the cases started.

It should be noted that I only get the number of cases initiated by the user with which the token was obtained, in my case the user is "admin"

I hope this information helps you
Attachments
image1.PNG
image1.PNG (54.15 KiB) Viewed 611 times
image2.PNG
image2.PNG (44.94 KiB) Viewed 611 times
image3.PNG
image3.PNG (117.88 KiB) Viewed 612 times

Hi, Tell your error here. Please check the foll[…]

Hi, I have a process that works fine on 3.2.1 -com[…]

Hi, You must change your codes like this: func[…]

issue with processmaker 3.2

From what I understand, you want to incorporate th[…]