1024programmer Java api-PHP calls the Java email service, and some garbled characters are found in the inbox. Could you please show me, thank you.

api-PHP calls the Java email service, and some garbled characters are found in the inbox. Could you please show me, thank you.

 $cOntent= 'live';
         $email = '[email protected]';

         $myHttp = new MyHttp();

         $data = array(
                 'tenantid' => UserLogicModel::getTenantId(),//tenant id
                 'suject' => $subject,
                 'text' => base64_encode($content),
                 'tomail' => $email,
                 'attachmentPath' => $attachmentPath, //"attachmentPath":"/usr/local/files/job offer notice.doc",
                 'attachmentName' => $attachmentName, //"attachmentName":"Entry offer notice.doc"
         );
        


         $json_data = json_encode($data);
         $params = "params=".$json_data;
         $res = $myHttp->send(Dict::$STATIC_SEND_MAIL_URL, $params, "POST", "");
class MyHttp {
    
     //Send network request
     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); //I know the HTTPSTAT code~
         curl_close($ch);
         switch ($stat) {
             case '200':
                 if(empty($tmpInfo))
                     $tmpInfo='{"stat":"ok"}';
                 break;
            
             default:
                 #code...
                 break;
         }
         if(empty($tmpInfo))
             $tmpInfo="";

         //Record the communication data with the api interface to the log
         \Think\Log::write("http $method req[$stat]:res($tmpInfo)",'ALERT');
         return $tmpInfo;
     }
 

The above is the curl code for requesting the java interface. No matter how I ask, I add specified charset=UTF-8, java side

 $head = array("Content-Type: application/json;charset=UTF-8"); //Solve the garbled problem of Java interface and directly throw json data
         curl_setopt($ch, CURLOPT_HTTPHEADER,$head);
         After adding this code, the parameters will not be accepted immediately.

Reply content:

 $cOntent= 'live';
         $email = '[email protected]';

         $myHttp = new MyHttp();

         $data = array(
                 'tenantid' => UserLogicModel::getTenantId(),//tenant id
                 'suject' => $subject,
                 'text' => base64_encode($content),
                 'tomail' => $email,
                 'attachmentPath' => $attachmentPath, //"attachmentPath":"/usr/local/files/job offer notice.doc",
                 'attachmentName' => $attachmentName, //"attachmentName":"Entry offer notice.doc"
         );
        


         $json_data = json_encode($data);
         $params = "params=".$json_data;
         $res = $myHttp->send(Dict::$STATIC_SEND_MAIL_URL, $params, "POST", "");
class MyHttp {
    
     //Send network request
     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); //I know the HTTPSTAT code~
         curl_close($ch);
         switch ($stat) {
             case '200':
                 if(empty($tmpInfo))
                     $tmpInfo='{"stat":"ok"}';break;
            
             default:
                 #code...
                 break;
         }
         if(empty($tmpInfo))
             $tmpInfo="";

         //Record the communication data with the api interface to the log
         \Think\Log::write("http $method req[$stat]:res($tmpInfo)",'ALERT');
         return $tmpInfo;
     }
 

The above is the curl code for requesting the java interface. No matter how I ask, I add specified charset=UTF-8, java side

 $head = array("Content-Type: application/json;charset=UTF-8"); //Solve the garbled problem of Java interface and directly throw json data
         curl_setopt($ch, CURLOPT_HTTPHEADER,$head);
         After adding this code, the parameters will not be accepted immediately.

In fact, it should be possible. I don’t know if your java interface stipulates that it must be in json format. If so, then modify this sentence:

$params = "params=".$json_data; //This is a string because you added params=this in front, so you can try sending $json_data directly

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/api-php-calls-the-java-email-service-and-some-garbled-characters-are-found-in-the-inbox-could-you-please-show-me-thank-you/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索