调用有道智云API,自动翻译WORDPRESS标题为英文

老虎说测试 脚本开发字数 2935阅读9分47秒阅读模式
摘要调用有道智云API,自动翻译WORDPRESS标题为英文。
调用有道智云API,自动翻译WORDPRESS标题为英文

wordpress

调用有道智云API,自动翻译WORDPRESS标题为英文。

代码:文章源自陈学虎-https://chenxuehu.com/article/2018/09/7278.html

<?php
/*
Plugin Name:zimeng_slug_translator
Plugin URI: https://blog.chenxuehu.com/
Description: 调用有道翻译API,自动将中文标题翻译成英语.
Version: 1.0.0
Author: Xuehu
Author URI:https://blog.chenxuehu.com/
License: GPL
*/

define("CURL_TIMEOUT",   20); 
define("URL",            "http://openapi.youdao.com/api"); 
define("APP_KEY",         ""); 
define("SEC_KEY",        "");

/**
 * 有道API翻译入口
 */
if( !function_exists("zimeng_youdao_translate")){
    function zimeng_youdao_translate($query, $from, $to)
    {
        $args = array(
            'q' => $query,
            'appKey' => APP_KEY,
            'salt' => rand(10000,99999),
            'from' => $from,
            'to' => $to,

        );
        $args['sign'] = md5(APP_KEY.$query.$args['salt'].SEC_KEY);
        $ret = zimeng_youdao_call(URL, $args);
        $ret = json_decode($ret, true);
        return $ret['translation'][0]; 
    }
}

if( !function_exists("zimeng_youdao_call")){
    function zimeng_youdao_call($url, $args=null, $method="post", $testflag = 0, $timeout = CURL_TIMEOUT, $headers=array())
    {
        $ret = false;
        $i = 0; 
        while($ret === false) 
        {
            if($i > 1)
                break;
            if($i > 0) 
            {
                sleep(1);
            }
            $ret = zimeng_youdao_callOnce($url, $args, $method, false, $timeout, $headers);
            $i++;
        }
        return $ret;
    }
}

if( !function_exists("zimeng_youdao_callOnce")){
    function zimeng_youdao_callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = CURL_TIMEOUT, $headers=array())
    {
        $ch = curl_init();
        if($method == "post") 
        {
            $data = zimeng_youdao_convert($args);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, 1);
        }
        else 
        {
            $data = zimeng_youdao_convert($args);
            if($data) 
            {
                if(stripos($url, "?") > 0) 
                {
                    $url .= "&$data";
                }
                else 
                {
                    $url .= "?$data";
                }
            }
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if(!empty($headers)) 
        {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if($withCookie)
        {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
        }
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }
}

if( !function_exists("zimeng_youdao_convert")){
    function zimeng_youdao_convert(&$args)
    {
        $data = '';
        if (is_array($args))
        {
            foreach ($args as $key=>$val)
            {
                if (is_array($val))
                {
                    foreach ($val as $k=>$v)
                    {
                        $data .= $key.'['.$k.']='.rawurlencode($v).'&';
                    }
                }
                else
                {
                    $data .="$key=".rawurlencode($val)."&";
                }
            }
            return trim($data, "&");
        }
        return $args;
    }
}

if( !function_exists("ZimengSlugTrans")){
    function ZimengSlugTrans($postID){
    global $wpdb;
    
    $tableposts = $wpdb->posts ;
    $sql = "SELECT post_title,post_name FROM $tableposts WHERE ID=$postID";
    $res = $wpdb->get_results($sql);    
    $post_title = $res[0]->post_title;
    $tran_title = zimeng_youdao_translate($post_title,"zh-CHS","EN");
    $slug = $tran_title;
    if(function_exists("sanitize_title") ) {
        if( sanitize_title( $res[0]->post_title ) != $res[0]->post_name  ){
            if( !substr_count($path, '%') ) 
                return true;
        }
        $slug = sanitize_title( $slug);
        if( strlen($slug) < 2 ) return true;
    }
        $sql ="UPDATE ".$tableposts." SET `post_name` = '".$slug."' WHERE ID =$postID;";        
        $res = $wpdb->query($sql);
            
    }   
}
 
add_action('publish_post', 'ZimengSlugTrans');
add_action('edit_post', 'ZimengSlugTrans');

 文章源自陈学虎-https://chenxuehu.com/article/2018/09/7278.html

做成了插件,但是没有做后台界面,要使用需要手动的修改文件,设置以下的信息为您的申请的信息:文章源自陈学虎-https://chenxuehu.com/article/2018/09/7278.html

define("APP_KEY",         ""); 
define("SEC_KEY",        "");

该翻译的API是付费的,酌情使用。文章源自陈学虎-https://chenxuehu.com/article/2018/09/7278.html

插件版本下载,修改信息后,后台启用插件即可。文章源自陈学虎-https://chenxuehu.com/article/2018/09/7278.html

 文章源自陈学虎-https://chenxuehu.com/article/2018/09/7278.html

 文章源自陈学虎-https://chenxuehu.com/article/2018/09/7278.html

下载信息 调用有道智云API,自动翻译WORDPRESS标题为英文 WORDPRESS
最近更新2018-9-24
网盘密码:发表评论并刷新可见
下载地址
文章源自陈学虎-https://chenxuehu.com/article/2018/09/7278.html文章源自陈学虎-https://chenxuehu.com/article/2018/09/7278.html

 
  • 版权声明:本文为原创文章,转载请附上原文出处链接及本声明。
  • 转载请注明:调用有道智云API,自动翻译WORDPRESS标题为英文 | https://chenxuehu.com/article/2018/09/7278.html