{PHP} Dictionary Web API

先来个 Google Translate API 自制版

Notice: This class has been out of data since Google Translate uses Ajax. Google Translate™ API PHP Wrapper is a better solution if you want to use Google Translate API. (updated on Sep 24 2010)

http://labs.involutive.com/2007/06/05/google-translator-api-php-5-class/

<?php
class Google_API_translator {
    public $opts = array("text" => "", "language_pair" => "en|it");
    public $out = "";

<span class="k">function</span> <span class="nf">__construct</span><span class="p">()</span> <span class="p">{}</span>

<span class="k">function</span> <span class="nf">setOpts</span><span class="p">(</span><span class="nv">$opts</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span><span class="p">(</span><span class="nv">$opts</span><span class="p">[</span><span class="s2">"text"</span><span class="p">]</span> <span class="o">!=</span> <span class="s2">""</span><span class="p">)</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">opts</span><span class="p">[</span><span class="s2">"text"</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$opts</span><span class="p">[</span><span class="s2">"text"</span><span class="p">];</span>
    <span class="k">if</span><span class="p">(</span><span class="nv">$opts</span><span class="p">[</span><span class="s2">"language_pair"</span><span class="p">]</span> <span class="o">!=</span> <span class="s2">""</span><span class="p">)</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">opts</span><span class="p">[</span><span class="s2">"language_pair"</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$opts</span><span class="p">[</span><span class="s2">"language_pair"</span><span class="p">];</span>
<span class="p">}</span>

<span class="k">function</span> <span class="nf">translate</span><span class="p">()</span> <span class="p">{</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">out</span> <span class="o">=</span> <span class="s2">""</span><span class="p">;</span>
    <span class="nv">$google_translator_url</span> <span class="o">=</span> <span class="s2">"http://google.com/translate_t?langpair="</span><span class="p">;</span>
    <span class="nv">$google_translator_url</span> <span class="o">.=</span> <span class="nb">urlencode</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">opts</span><span class="p">[</span><span class="s2">"language_pair"</span><span class="p">])</span><span class="o">.</span><span class="s2">"&amp;#038;"</span><span class="p">;</span>
    <span class="nv">$google_translator_url</span> <span class="o">.=</span> <span class="s2">"text="</span><span class="o">.</span><span class="nb">urlencode</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">opts</span><span class="p">[</span><span class="s2">"text"</span><span class="p">]);</span>
    <span class="nv">$gphtml</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getPage</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s2">"url"</span> <span class="o">=&gt;</span> <span class="nv">$google_translator_url</span><span class="p">));</span>
    <span class="nb">preg_match</span><span class="p">(</span><span class="s1">'/(.*?)/'</span><span class="p">,</span> <span class="nv">$gphtml</span><span class="p">,</span> <span class="nv">$out</span><span class="p">);</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">out</span> <span class="o">=</span> <span class="nb">utf8_encode</span><span class="p">(</span><span class="nv">$out</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
    <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">out</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">function</span> <span class="nf">getPage</span><span class="p">(</span><span class="nv">$opts</span><span class="p">)</span> <span class="p">{</span>
    <span class="nv">$html</span> <span class="o">=</span> <span class="s2">""</span><span class="p">;</span>
    <span class="k">if</span><span class="p">(</span><span class="nv">$opts</span><span class="p">[</span><span class="s2">"url"</span><span class="p">]</span> <span class="o">!=</span> <span class="s2">""</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$ch</span> <span class="o">=</span> <span class="nb">curl_init</span><span class="p">(</span><span class="nv">$opts</span><span class="p">[</span><span class="s2">"url"</span><span class="p">]);</span>
        <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$ch</span><span class="p">,</span> <span class="nx">CURLOPT_RETURNTRANSFER</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
        <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$ch</span><span class="p">,</span> <span class="nx">CURLOPT_HEADER</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
        <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$ch</span><span class="p">,</span> <span class="nx">CURLOPT_FOLLOWLOCATION</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
        <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$ch</span><span class="p">,</span> <span class="nx">CURLOPT_TIMEOUT</span><span class="p">,</span> <span class="mi">15</span><span class="p">);</span>
        <span class="nv">$html</span> <span class="o">=</span> <span class="nb">curl_exec</span><span class="p">(</span><span class="nv">$ch</span><span class="p">);</span>
        <span class="k">if</span><span class="p">(</span><span class="nb">curl_errno</span><span class="p">(</span><span class="nv">$ch</span><span class="p">))</span> <span class="p">{</span>
            <span class="nv">$html</span> <span class="o">=</span> <span class="s2">""</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="nb">curl_close</span> <span class="p">(</span><span class="nv">$ch</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="nv">$html</span><span class="p">;</span>
<span class="p">}</span>

}

// Example $g = new Google_API_translator(); $g->setOpts(array("text" => "ciao", "language_pair" => "it|en")); $g->translate(); echo $g->out; ?>

在线词典 API:

dict.cn: http://api.dict.cn/ws.php?utf8=true&q=#word

XML 格式。Project Vaynwords 之前一直在用,说实话查询质量一般。

爱词霸: http://dict-co.iciba.com/api/dictionary.php?w=#word

XML格式。

QQ 词典: http://dict.qq.com/dict?q=#word

JSON 格式。