1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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/