gpt4 book ai didi

PHP 和 Bing 翻译 API

转载 作者:可可西里 更新时间:2023-11-01 13:21:33 28 4
gpt4 key购买 nike

我已经在 Azure Marketplace 注册,我有一个客户端 ID 和一个客户端“ secret ”,但到目前为止我尝试的所有操作仍然会导致“400 Bad Request”错误。非常感谢!

这是我一直在尝试的代码的一个相当基本的示例(我已经编辑了客户端 ID 和 secret 值)。我的理解是可以通过 URL 请求传递 post 变量……我希望这是正确的。

$authURL = 'http://datamarket.accesscontrol.windows.net/v2/OAuth2-13&grant_type=client_credentials&client_id={CLIENT_ID VALUE HERE}&client_secret={CLIENT_SECRET VALUE HERE}&scope=http://api.microsofttranslator.com';
$chpre = curl_init();
curl_setopt($chpre, CURLOPT_URL, $authURL );
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$xpre = curl_exec($chpre);

$texttobetranslated = "الذي تقدمه";
$BingURL = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" . $texttobetranslated . "&from=ar&to=en";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $BingURL );
$x = curl_exec($ch);

最佳答案

我创建了一个易于使用且易于集成到任何 PHP 项目的小型 PHP 类。你可以找到它here .这是代码:

  <?php
class BingTranslation
{
public $clientID;
public $clientSecret;

public function __construct($cid, $secret)
{
$this->clientID = $cid;
$this->clientSecret = $secret;
}

public function get_access_token()
{
//if access token is not expired and is stored in COOKIE
if(isset($_COOKIE['bing_access_token']))
return $_COOKIE['bing_access_token'];

// Get a 10-minute access token for Microsoft Translator API.
$url = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13';
$postParams = 'grant_type=client_credentials&client_id='.urlencode($this->clientID).
'&client_secret='.urlencode($this->clientSecret).'&scope=http://api.microsofttranslator.com';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rsp = curl_exec($ch);
$rsp = json_decode($rsp);
$access_token = $rsp->access_token;

setcookie('bing_access_token', $access_token, $rsp->expires_in);

return $access_token;
}

public function translate($word, $from, $to)
{
$access_token = $this->get_access_token();
$url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate?text='.$word.'&from='.$from.'&to='.$to;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:bearer '.$access_token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rsp = curl_exec($ch);

preg_match_all('/<string (.*?)>(.*?)<\/string>/s', $rsp, $matches);

return $matches[2][0];
}

public function translate2($word, $from, $tos)
{
//translates 1 word to several languages
//$tos is array of languages to translate to
//returns array of translations as $result['en']=>'Hello'

$access_token = $this->get_access_token();

$result[$from] = $word;

foreach($tos as $to)
{
$url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate?text=hello&from='.$from.'&to='.$to;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:bearer '.$access_token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rsp = curl_exec($ch);

preg_match_all('/<string (.*?)>(.*?)<\/string>/s', $rsp, $matches);

$result[$to] = $matches[2][0];
}

return $result;
}
}
?>

关于PHP 和 Bing 翻译 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10749019/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com