$content = '活生生的'; $email = 'test_email@gmail.com'; $myHttp = new MyHttp(); $data = array( 'tenantid' => UserLogicModel::getTenantId(),//租戶id 'suject' => $subject, 'text' => base64_encode($content), 'tomail' => $email, 'attachmentPath' => $attachmentPath, //"attachmentPath":"/usr/local/files/入職offer通知.doc", 'attachmentName' => $attachmentName, //"attachmentName":"入職offer通知.doc" ); $json_data = json_encode($data); $params = "params=".$json_data; $res = $myHttp->send(Dict::$STATIC_SEND_MAIL_URL, $params, "POST", "");
class MyHttp { //發送網路請求 public function send($url,$data,$method,$token){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //$header = array( // 'u:1234567777', // 't:1234568888', //); curl_setopt($ch, CURLOPT_HTTPHEADER, array('token:$token')); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } $stat=curl_getinfo($ch,CURLINFO_HTTP_CODE); //我知道HTTPSTAT碼哦~ curl_close($ch); switch ($stat) { case '200': if(empty($tmpInfo)) $tmpInfo='{"stat":"ok"}'; break; default: # code... break; } if(empty($tmpInfo)) $tmpInfo=""; //記錄與api介面之間的通訊資料到日誌 \Think\Log::write("http $method req[$stat]:res($tmpInfo)",'ALERT'); return $tmpInfo; }
上面是請求java介面的curl代碼,無論我怎麼求裡面加指定charset=UTF-8,java端
$head = array("Content-Type: application/json;charset=UTF-8"); //解決java介面亂碼問題,直接拋送json資料 curl_setopt($ch, CURLOPT_HTTPHEADER,$head); 加上這句代碼,馬上就接受不到參數.
回複內容:
$content = '活生生的'; $email = 'test_email@gmail.com'; $myHttp = new MyHttp(); $data = array( 'tenantid' => UserLogicModel::getTenantId(),//租戶id 'suject' => $subject, 'text' => base64_encode($content), 'tomail' => $email, 'attachmentPath' => $attachmentPath, //"attachmentPath":"/usr/local/files/入職offer通知.doc", 'attachmentName' => $attachmentName, //"attachmentName":"入職offer通知.doc" ); $json_data = json_encode($data); $params = "params=".$json_data; $res = $myHttp->send(Dict::$STATIC_SEND_MAIL_URL, $params, "POST", "");
class MyHttp { //發送網路請求 public function send($url,$data,$method,$token){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //$header = array( // 'u:1234567777', // 't:1234568888', //); curl_setopt($ch, CURLOPT_HTTPHEADER, array('token:$token')); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } $stat=curl_getinfo($ch,CURLINFO_HTTP_CODE); //我知道HTTPSTAT碼哦~ curl_close($ch); switch ($stat) { case '200': if(empty($tmpInfo)) $tmpInfo='{"stat":"ok"}'; break; default: # code... break; } if(empty($tmpInfo)) $tmpInfo=""; //記錄與api介面之間的通訊資料到日誌 \Think\Log::write("http $method req[$stat]:res($tmpInfo)",'ALERT'); return $tmpInfo; }
上面是請求java介面的curl代碼,無論我怎麼求裡面加指定charset=UTF-8,java端
$head = array("Content-Type: application/json;charset=UTF-8"); //解決java介面亂碼問題,直接拋送json資料 curl_setopt($ch, CURLOPT_HTTPHEADER,$head); 加上這句代碼,馬上就接受不到參數.
其實應該是可以得,不知道你的java介面是不是規定了必須是json格式的,如果是,那麼你這一句修改下:
$params = "params=".$json_data; //你這是一個string,因為你前面加了params=這個,所以你直接把$json_data傳送過去試試