php百度api調用簡單一實例

來源:互聯網
上載者:User

標籤:

<html><head>    <title>腦筋急轉彎</title>    <meta charset="utf-8"></head><body><?php$ch = curl_init();$url = ‘http://apis.baidu.com/txapi/naowan/naowan‘;$header = array(    ‘apikey:xxxxxxxxxxxxxxxxxxxxx‘,   //需要百度apikey);// 添加apikey到headercurl_setopt($ch, CURLOPT_HTTPHEADER, $header);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 執行HTTP請求curl_setopt($ch, CURLOPT_URL, $url);$res = curl_exec($ch);$con = json_decode($res, true);foreach ($con as $v) {    if (is_array($v)) {        foreach ($v as $c) {            $result = $c[‘result‘];            echo $result;            $result = urlencode($result);//將關鍵字編碼            $result = preg_replace("/(%7E|%60|%21|%40|%23|%24|%25|%5E|%26|%27|%2A|%28|%29|%2B|%7C|%5C|%3D|\-|_|%5B|%5D|%7D|%7B|%3B|%22|%3A|%3F|%3E|%3C|%2C|\.|%2F|%A3%BF|%A1%B7|%A1%B6|%A1%A2|%A1%A3|%A3%AC|%7D|%A1%B0|%A3%BA|%A3%BB|%A1%AE|%A1%AF|%A1%B1|%A3%FC|%A3%BD|%A1%AA|%A3%A9|%A3%A8|%A1%AD|%A3%A4|%A1%A4|%A3%A1|%E3%80%82|%EF%BC%81|%EF%BC%8C|%EF%BC%9B|%EF%BC%9F|%EF%BC%9A|%E3%80%81|%E2%80%A6%E2%80%A6|%E2%80%9D|%E2%80%9C|%E2%80%98|%E2%80%99)+/", ‘‘, $result);            $result = urldecode($result);//將過濾後的關鍵字解碼            ?>            <form method="post" action="">                <table border="1">                    <tr>                        <td>id:<?php echo $c[‘id‘]; ?></td>                    </tr>                    <tr>                        <td><?php echo $c[‘quest‘]; ?></td>                    </tr>                    <tr>                        <td><?php echo $result; ?></td>                    </tr>                </table>            </form>            <?php        }    }}?></body></html>

 

php百度api調用簡單一實例

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.