Как передать REST API параметр типа Integer?

При обращении с REST API Битрикс24 из php нужно передать параметр TASKID
Если делаю через 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);

тогда возвращается ошибка Param #0 (taskId) for method ctaskitem::getdata() expected to be of type "integer", but given something else
Переменная $number типа Integer

Если делаю через 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;

тогда всё работает, но не имею возможности отловить ошибку, в случае, если будет неверно введен номер задачи

Как передать через cURL параметр с типом Integer?
  • Вопрос задан
  • 681 просмотр
Пригласить эксперта
Ответы на вопрос 2
@dimoff66
Кратко о себе: Я есть
Попробуйте в строке
$service_url = 'https://'.HomeController::DOMAIN.'.bitrix24.ru/rest/71/##############/task.item.getdata.json';


поставить слэш после json
Ответ написан
Комментировать
gromdron
@gromdron Куратор тега Битрикс24
Работаю с Bitrix24
Вы не правильно составляете запрос. Вы посылаете не TASKID = 2, а структуру из content которая содежит json от http_build_query и уже нужного массива.

Замените:
$data = array('TASKID' => $number);
$curl_post_data = array(
    'content' => http_build_query($data)
);
$curl_post_data = json_encode($curl_post_data);


На:
$curl_post_data = array('TASKID' => $number);
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы