This commit is contained in:
jhx
2022-08-30 11:18:07 +08:00
parent 1689dfffe9
commit e367f4b8d1
5 changed files with 659 additions and 2 deletions
+34
View File
@@ -3,6 +3,8 @@
namespace app\controller;
use App\model\Ip2locationDb11Model;
use app\server\GuzzleServer;
use GuzzleHttp\RequestOptions;
use itbdw\Ip\IpLocation;
use support\Db;
use support\Request;
@@ -61,4 +63,36 @@ class Index
'google' => $mapUrl
]], JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
}
public function translate(Request $request)
{
$p = $request->get('p');
$sl = $request->get('sl', 'auto');
$tl = $request->get('tl', 'zh-cn');
$client = GuzzleServer::getClient();
$url = "https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl={$sl}&tl={$tl}&q={$p}";
$proxyList = [
0 => 'woon:woon@146.56.181.114:6666',
1 => 'woon:woon@107.189.31.119:6666',
2 => 'woon:woon@132.145.81.233:6666',
3 => 'woon:woon@45.66.131.147:6666',
];
$proxy = $proxyList[array_rand($proxyList)];
$response = $client->post($url, [
'timeout' => 10,
'proxy' => $proxy
]);
$response = $response->getBody()->getContents();
$responseArr = json_decode($response, true);
return json($responseArr);
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
/**
*
* @author jhx
* @date 2021/9/28 15:45
*/
namespace app\server;
use GuzzleHttp\Client;
class GuzzleServer
{
public function __construct()
{
}
/**
* singleton
*/
protected static $instanceClient = null;
/**
* destruct.
*/
public function __destruct()
{
static::$instanceClient = null;
}
/**
* 获取单例
* @param array $config 配置参数
*/
public static function getClient($config = []): Client
{
if (!(static::$instanceClient instanceof Client)) {
static::$instanceClient = new Client($config);
}
return static::$instanceClient;
}
}