protected function create_page_from_postdata()

in classes/api/base.php [1120:1173]


    protected function create_page_from_postdata($sectionid, $postdata, $boundary) {
        try {
            if (\local_o365\rest\unified::is_configured() === true) {
                $endpoint = '/sections/'.$sectionid.'/pages';
                $options['contenttype'] = 'multipart/form-data; boundary='.$boundary;
                $response = $this->apicall('post', $endpoint, $postdata, $options);
                $response = $this->process_apicall_response($response, ['id' => null]);
                // Deep convert array to object.
                $response = json_decode(json_encode($response));
                return $response;
            } else {
                $url = static::API.'/sections/'.$sectionid.'/pages';

                $token = $this->get_token();
                if (empty($token)) {
                    \local_onenote\utils::debug('Could not get user token', 'create_page_from_postdata');
                    return null;
                }

                $curl = new \curl();

                $headers = [
                    'Content-Type: multipart/form-data; boundary='.$boundary,
                    'Authorization: Bearer '.rawurlencode($token),
                ];
                foreach ($headers as $header) {
                    $curl->setHeader($header);
                }

                $rawresponse = $curl->post($url, $postdata, ['CURLOPT_HEADER' => 1]);

                // Check if curl call fails.
                if ($curl->errno != CURLE_OK) {
                    $errorstr = 'curl error '.$curl->errno.': '.$curl->error;
                    \local_onenote\utils::debug('curl connection error', 'onenote\api\create_page_from_postdata', $errorstr);
                    // If curl call fails and reason is net connectivity return it or return null.
                    return (in_array($curl->errno, ['6', '7', '28'])) ? 'connection_error' : null;
                }

                if ($curl->info['http_code'] == 201) {
                    $responsewithoutheader = substr($rawresponse, $curl->info['header_size']);
                    $response = json_decode($responsewithoutheader);
                    return $response;
                } else {
                    $debugdata = ['curlinfo' => $curl->info, 'postdata' => $postdata];
                    \local_onenote\utils::debug('problem creating page', 'onenote\api\create_page_from_postdata', $debugdata);
                }
            }
        } catch (\Exception $e) {
            \local_onenote\utils::debug($e->getMessage(), 'create_page_from_postdata', $e);
        }

        return null;
    }