Heim >Backend-Entwicklung >PHP-Tutorial >api - PHP调用java发邮件的服务,收件箱发现部分乱码,麻烦大家给我看看,谢谢

api - PHP调用java发邮件的服务,收件箱发现部分乱码,麻烦大家给我看看,谢谢

WBOY
WBOYOriginal
2016-06-06 20:12:141191Durchsuche

<code>        $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", "");</code>
<code>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;
    }
</code>

上面是请求java接口的curl代码,无论我怎么求里面加指定charset=UTF-8,java端

<code> $head    = array("Content-Type: application/json;charset=UTF-8"); //解决java接口乱码问题,直接抛送json数据
        curl_setopt($ch, CURLOPT_HTTPHEADER,$head);
        加上这句代码,马上就接受不到参数.</code>

回复内容:

<code>        $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", "");</code>
<code>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;
    }
</code>

上面是请求java接口的curl代码,无论我怎么求里面加指定charset=UTF-8,java端

<code> $head    = array("Content-Type: application/json;charset=UTF-8"); //解决java接口乱码问题,直接抛送json数据
        curl_setopt($ch, CURLOPT_HTTPHEADER,$head);
        加上这句代码,马上就接受不到参数.</code>

其实应该是可以得,不知道你的java接口是不是规定了必须是json格式的,如果是,那么你这一句修改下:

<code>$params    = "params=".$json_data;    //你这是一个string,因为你前面加了params=这个,所以你直接把$json_data传送过去试试</code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:javascript - 想学下vue.jsNächster Artikel:PHP中数组的定义方式