cURL to post JSON data

    static function post_json($url, $json, $header_params=null){
        if(is_object($json) || is_array($json)){
            $json = json_encode($json);
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, FALSE);//回傳資料不包含header

        $header = [
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($json)
        ];
        if($header_params != false){
            $header = array_merge($header, $header_params);
        }
        logg($header);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

        $errors = curl_error($ch);
        //logg($errors);

        $data = curl_exec($ch);

        //$info = curl_getinfo($ch);
        curl_close ($ch);

        $res = [
            'data' => $data, 
            'error' => $errors
        ];

        return $res;
    }

https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/199968/