golang 调用php程序

遇到的问题

i am try to change a fuction from php to golang . the function job is use chr,ord,base4_encode to encode some string。php generate a serial int number, like 122|234|135|138|179|19|190|183|80|156|4|159|195|213|86|241|140|7|112|23|61|182|37|91|185|26|203|185|206|206|183, some number biger than 127,the ascii bigest number is 127. now, the probrem is: php's chr(206) is not equivalent golang's string(rune(206))

please help me,thx

The results in PHP and Go are different because, as the documentation for each states, PHP's chr returns the ASCII character for its argument, whereas Go's rune uses UTF-8. Below 127, ASCII and UTF-8 are the same, but above that, they differ.

PHP和Go中的结果是不同的,因为作为每种状态的文档,PHP的chr返回其参数的ASCII字符,而Go的符文 使用UTF-8。低于127,ASCII和UTF-8相同,但高于ASCII和UTF-8,则不同。

为了和php一直则需要

  • 一个可以执行的php文件 think_encrypt.php
<?php

    function think_encrypt($data, $key = '1231231', $expire = 0) {
        $key  = md5($key );
        $data = base64_encode($data);
        $x    = 0;
        $len  = strlen($data);
        $l    = strlen($key);
        $char = '';

        for ($i = 0; $i < $len; $i++) {
            if ($x == $l) $x = 0;
            $char .= substr($key, $x, 1);
            $x++;
        }

        $str = sprintf('%010d', $expire ? $expire + time():0);

        for ($i = 0; $i < $len; $i++) {
            $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1)))%256);
        }
        return str_replace(array('+','/','='),array('-','_',''),base64_encode($str));
    }

    $param_arr = getopt('a:');
    echo think_encrypt($param_arr["a"]);
?>
  • go程序
/**
* @Title ThinkEncrypt
* @Description:  调php命令进行加密
* @Param:
* @return:
* @Author: liwei
* @Date: 2020/6/8
**/
func ThinkEncrypt(data string,key string,expore int) string  {
    cmd := exec.Command("/bin/bash", "-c", os.Getenv("PHP_PATH")+" extension/php/think_encrypt.php -a "+data)
    stdout, _ := cmd.StdoutPipe()   //创建输出管道
    defer stdout.Close()
    if err := cmd.Start(); err != nil {
        log.Fatalf("cmd.Start: %v")
    }
    result, _ := ioutil.ReadAll(stdout) // 读取输出结果
    resdata := string(result)
    fmt.Print(resdata)
    return resdata
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容