如何用rest api在new case时设置变量的值

中文论坛 (Forum for Chinese Speakers)

Moderators: amosbatto, yuhesky

xiaobai
Posts: 19
Joined: Wed Oct 18, 2017 4:26 am
Country: China

如何用rest api在new case时设置变量的值

Unread postby xiaobai » Tue Nov 07, 2017 5:03 am

文档里没有具体说明,我用以下方法传入post参数会出错:

Code: Select all

   $variables = json_encode(array(
            "dataTable" => $dataTable,
            "dataId" => $dataId           
        ));
         $postParams = array(
            'pro_uid'    => $processId,
            'tas_uid'    => $firstTaskId,
            'variables'  => $variables
        );

amosbatto
Posts: 5566
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz

Re: 如何用rest api在new case时设置变量的值

Unread postby amosbatto » Tue Nov 07, 2017 11:02 pm

What is the rest of your code?
You need to use http_build_query() when sending the variables.
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

xiaobai
Posts: 19
Joined: Wed Oct 18, 2017 4:26 am
Country: China

Re: 如何用rest api在new case时设置变量的值

Unread postby xiaobai » Tue Nov 07, 2017 11:39 pm

amosbatto wrote:What is the rest of your code?
You need to use http_build_query() when sending the variables.

Code: Select all

public function startCase($processId,$firstTaskId,$dataTable,$dataId){
        $user = $this->username;
        $pwd = $this->passward;
        //请求地址从配置获取并组装!!!!!!!
        //$url = "http://localhost:8000/api/1.0/workflow/cases";
        $url =  self::PM_HOST.self::PM_RESTAPI_PATH.self::PM_WORK_SPACE."/cases";
        //传入对应的业务数据
        $variables = json_encode(array(
            "dataTable" => $dataTable,
            "dataId" => $dataId           
        ));
        $postParams = array(
            'pro_uid'    => $processId,
            'tas_uid'    => $firstTaskId,
            'variables'  => $variables
        );
        $oToken = $this->getPMAccessToken($user, $pwd);
        $accessToken =  $oToken->access_token;
        $ret = $this->pmRestRequest("POST",$url,$postParams,$accessToken);
        return $ret;
   
    }
   


Code: Select all

  public function pmRestRequest($method = "GET",$url,$postParams = null,$accessToken){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$accessToken));
   
        switch ($method){
            case "GET" :
                curl_setopt($ch, CURLOPT_HTTPGET, true);
                break;
            case "POST":
                curl_setopt($ch, CURLOPT_POST,true);
                curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($postParams));
                break;
            case "PUT" :
                curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($postParams));
                break;
            case "PATCH":
                curl_setopt($ch, CULROPT_CUSTOMREQUEST, 'PATCH');
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
                break;
            case "DELETE":
                curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                curl_setopt($ch, CURLOPT_POSTFIELDS,$postParams);
                break;
        }
   
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ret = json_decode(curl_exec($ch));
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $return = array(
            "httpCode"  => $statusCode,
            "returnValue" => $ret
        );
        return $return;
    }


I have used http_build_query(),but the response still raise error:400 Bad Request: Invalid value specified for `variables`

xiaobai
Posts: 19
Joined: Wed Oct 18, 2017 4:26 am
Country: China

Re: 如何用rest api在new case时设置变量的值

Unread postby xiaobai » Wed Nov 08, 2017 12:23 am

Code: Select all

 $postParams = array(
            'pro_uid'    => $processId,
            'tas_uid'    => $firstTaskId,
            'variables'  => $variables
        );


我把这里的参数名variables改成variable,执行成功,生成了新的case,返回值200,但是变量并没有设置成功

Code: Select all

 $postParams = array(
            'pro_uid'    => $processId,
            'tas_uid'    => $firstTaskId,
            'variable'  => $variables
        );

amosbatto
Posts: 5566
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz

Re: 如何用rest api在new case时设置变量的值

Unread postby amosbatto » Wed Nov 08, 2017 10:31 pm

Don't use json_encode().

You need to use this format to create a new case with variables:

Code: Select all


$processId 
= '49433188659fd0135827d19009479146';
$taskId = '56257615159fd021fa81349070852612';
$url = "/api/1.0/workflow/cases";

$aParams = array(
  'pro_uid'   => $processId,
  'tas_uid'   => $taskId,
  'variables' => array(array(
    'name'         => 'John Doe',                      //textbox
    'address'      => "636 Main St.\nPlano, TX 34356", //textarea, with \n for line break               
    'country'      => 'MX',                            //dropdown, selected value
    'country_label'=> 'Mexico',                        //dropdown, selected label (optional)
    'hasContract'  => array('1'),                      //checkbox, array('1') to mark, array('0') to unmark
    'selectProduct'=> 'cookies',                       //radiogroup, selected value (not label)
    'selectStates' => array('IN', 'WI'),               //checkgroup, selected values in an array
    'dueDate'      => '2018-11-25 23:59:59',           //datetime, in 'YYYY-MM-DD HH:MM:SS' format
    'orderList'    => array(                           //grid with textbox, checkbox & dropdown
      '1' => array('price'=>23.45, 'inCash'=>'1', 'creditCard'=>'Visa'),
      '2' => array('price'=>56.74, 'inCash'=>'0', 'creditCard'=>'Discover') 
    
) 
    
//Note: checkboxes inside grids have values of '1' or '0', not array('1') or array('0') 
    //like checkboxes outside grids
  ))
);
                  

$oRet 
= pmRestRequest("POST", $url, $aParams, $oToken->access_token);
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

amosbatto
Posts: 5566
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz

Re: 如何用rest api在new case时设置变量的值

Unread postby amosbatto » Wed Nov 08, 2017 10:36 pm

Somebody deleted the code examples from the wiki!

Here is the old documentation which shows how to call this endpoint with PHP:
https://web.archive.org/web/20170115203045/http://wiki.processmaker.com/3.0/REST_API_Cases/#New_Case:_POST_.2Fcases
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

xiaobai
Posts: 19
Joined: Wed Oct 18, 2017 4:26 am
Country: China

Re: 如何用rest api在new case时设置变量的值

Unread postby xiaobai » Thu Nov 09, 2017 10:12 am

amosbatto wrote:Somebody deleted the code examples from the wiki!

Here is the old documentation which shows how to call this endpoint with PHP:
https://web.archive.org/web/20170115203045/http://wiki.processmaker.com/3.0/REST_API_Cases/#New_Case:_POST_.2Fcases


Thanks a lot,my friend,it worked!


Return to “中文 (Chinese)”

Who is online

Users browsing this forum: No registered users and 1 guest