D
D
Dmitry Shelygin2018-11-30 20:33:03
API
Dmitry Shelygin, 2018-11-30 20:33:03

How to pass Integer type parameter to REST API?

When accessing the Bitrix24 REST API from php, you need to pass the TASKID parameter
If I do it via cURL

$service_url = 'https://'.HomeController::DOMAIN.'.bitrix24.ru/rest/71/##############/task.item.getdata.json';
        $curl = curl_init($service_url);
 
        $data = array('TASKID' => $number);
 
        $curl_post_data = array(
            'content' => http_build_query($data)
        );
        $curl_post_data = json_encode($curl_post_data);
        
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
        $curl_response = curl_exec($curl);
        if ($curl_response === false) {
            $info = curl_getinfo($curl);
            curl_close($curl);
            die('error occured during curl exec. Additioanl info: ' . var_export($info));
        }
        curl_close($curl);
        $decoded = json_decode($curl_response);
        if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
            die('error occured: ' . $decoded->response->errormessage);
        }
        dd($decoded);

then an error is returned Param #0 (taskId) for method ctaskitem::getdata() expected to be of type "integer", but given something else $number
variable of type Integer If done via file_get_contents

queryUrl = 'https://'.HomeController::DOMAIN.'.bitrix24.ru/rest/71/#####################/task.item.getdata.json';
 
        $data = array('TASKID' => $number);
        $options = array(
            'http' => array(
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query($data)
            )
        );
        $context  = stream_context_create($options);
        $result = file_get_contents($queryUrl, false, $context);
        dd($result);
        $result = json_decode($result, 1);
 
        return $result;

then everything works, but I have no way to catch an error if the task number is entered incorrectly.
How can I pass a parameter with the Integer type via cURL?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-11-30
@dimoff66

Try in line
put slash after json

A
Andrey Nikolaev, 2018-12-12
@gromdron

You are not making the request correctly. You are not sending TASKID = 2, but a structure from content that contains json from http_build_query and the already needed array.
Replace:

$data = array('TASKID' => $number);
$curl_post_data = array(
    'content' => http_build_query($data)
);
$curl_post_data = json_encode($curl_post_data);

On the:
$curl_post_data = array('TASKID' => $number);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question