api - PHP調用java發郵件的服務,收件匣發現部分亂碼,麻煩大家給我看看,謝謝

來源:互聯網
上載者:User
關鍵字 php java api
        $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傳送過去試試
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.