PHP Google の翻訳 API コード_PHP チュートリアル

WBOY
リリース: 2016-07-21 15:48:37
オリジナル
704 人が閲覧しました

新しく作成された ANSI の PHP 文件、その後作成された種類:

复制代码代码如下:

header("Content-Type: text/html; charset=utf-8");
class Google_API_translator{
public $opts = array("text" => "", " language_pair" => "en|it");
パブリック $out = "";
function setOpts($opts) {
if($opts["text"] != "") $this->opts["text"] = $opts["text"];
if($opts["言語ペア"] != "") $this->opts["言語ペア"] = $opts["言語ペア"];
}
function translation() {
$this->out = "";
$google_translator_url = "http://translate.google.com/translate_t?langpair=".urlencode($this->opts[" language_pair"])."&;";
$google_translator_data .= "text=".urlencode($this->opts["text"]);
$gphtml = $this->postPage(array("url" => $google_translator_url, "data" => $google_translator_data));
$out = substr($gphtml, strpos($gphtml, "
"));
$out = substr($out, 29);
$out = substr($out, 0, strpos($out, "
"));
$this->out = utf8_encode($out);
$this->out を返す;
}
関数 postPage($opts) {
$html ='';
if($opts["url"] != "" && $opts["data"] != "") {
$ch =curl_init($opts["url"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $opts["data"]);
$html =curl_exec($ch);
if(curl_errno($ch)) $html = "";
curl_close ($ch);
}
$html を返す;
}
}
?>

使用時の候
复制代码代码如下:

$g = new Google_API_translator();
$g->setOpts(array("text" => "Cjjer是天才", " language_pair" => "zh-CN|en"));
$g->translate();
echo $g->out;
?>
これは可能です、出力:Cjjer は天才です
PHP のここでは、部分的に同等の部分コードを参照してください。具体的には忘れられています。

http://www.bkjia.com/PHPjc/319695.html

tru​​ehttp://www.bkjia.com/PHPjc/319695.html技術記事 ANSI の PHP ファイルを新しく作成し、その後次の種類を作成します。 class Google_API_translator{ public $opts = arra...
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!