百度api翻译小demo

注:此demo主要批量更新地里位置信息的一个案例,循环请求,将获得数据更新到数据库

//查询数据的值
 $res = Db::name('cn_base_district')
            //->where('district_name_us','')
            ->where('district_name_us', '')
            ->where('district_is_level', '=', '3')
            ->select();
$curl = new Curl;      //实例化curl请求
 foreach ($res as $key => $value) {
            //拼接appid=2015063000000001+q=apple+salt=1435660288+密钥=12345678
            //得到字符串1 =2015063000000001apple143566028812345678
            $appid = '20190117000257404';
            $salt = time();
            $key = 'gEa6v9FBvsHfHhkTOwWE';
            $content = $value['district_name'];
            $sign = md5($appid . $content . $salt . $key);   //组装签名
            $url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?q=' . $content . '&from=zh&to=en&appid=' . $appid . '&salt=' . $salt . '&sign=' . $sign; //得到请求地址
            $remote = $curl->post($url, []);
            $remote = json_decode($remote, true);
            $us_name = $remote['trans_result'][0]['dst'];
            Db::name('cn_base_district')
                ->where('district_id', $value['district_id'])
                ->update(['district_name_us' => $us_name]);

        }

你可能感兴趣的:(PHP技术总结)