Yii的RestApi的使用方法

其他资料
1.设置美化路由和规则:
2.设置 .htaccess文件在app\web.htaccess文件中:
3.利用gii生成 MVC(Model,View,Controller)
4.访问api方式

1.设置美化路由和规则:

    'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'showScriptName' => false,
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,// 美化url
        'rules' => [
           '' => 'site/index',
            // ['class' => 'yii\rest\UrlRule', 'controller' => 'authapi'],
           ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
           ['class' => 'yii\rest\UrlRule', 'controller' => 'player'],
           [
               'class' => 'yii\rest\UrlRule',
               'controller' => 'api',
               'pluralize' => false,
               'extraPatterns' => [
               'GET bntaketranslateapi' => 'bntaketranslateapi', // 自定义GET 方式 actionBntaketranslateapi
                ],
           ],
        //    ['class' => 'yii\rest\UrlRule', 'controller' => 'v1/goods', 'pluralize' => false],

        ],
    ],

2.设置 .htaccess文件在app\web\.htaccess文件中:

Options +FollowSymLinks
IndexIgnore /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

3.利用gii生成 MVC(Model,View,Controller)
ApiController.php

 'yii\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];
    public function checkAccess()
    {
    }

    public function actionBntaketranslateapi($msg = "hello world",$source="en",$target="zh" ){
        $bntakeAPiTranslate = new BntakeAPi();
        $model =  $bntakeAPiTranslate->TranslateENtoZH($msg,$source,$target);
        return $model;
        // return $this->render('/staff/testView',[
        //     'model' =>$model,
        // ]);
    }

}
?>

自定义Model

        translate('Hello World!');
        $ka_translateArray = $tr->translate(['I can dance','I fine','And you']);
        // $tr->setHttpOption(['proxy' => 'tcp://localhost:8887'])->translate('Hello');
        Yii::trace($ka_translate, 'lucky');
        Yii::trace($ka_translateArray,'lucky');
        $rawResult = $tr->getResponse('I can dance');
        Yii::trace($tr->translate(['I can dance', 'I like trains', 'Double rainbow']),'lucky');

        Yii::trace( TranslateClient::translate('en', 'zh', 'Hello again'),'lucky');

        // $tr->setHttpOption(['proxy' => 'tcp://localhost:8888'])->translate('Hello');
        // echo
        return $tr->translate('Hello World!');
    }

    // public function TranslateENtoZH($msg){
    //     $tr = new TranslateClient('en', 'zh');
    //     $ka_translate = $tr->translate($msg);
    //     return $ka_translate;
    // }

    public function TranslateENtoZH($msg,$source,$target){
        $apiKey = 'AIzaSyCtNLSiRhTn-TA0hk1XwKEC5XBFxNMFVwU';
        $text = $msg;
        $url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($text) . '&source=' . $source . '&target=' . $target;

        $handle = curl_init($url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($handle);
        $responseDecoded = json_decode($response, true);
        $responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);      //Here we fetch the HTTP response code
        curl_close($handle);

        if($responseCode != 200) {
            // echo 'Fetching translation failed! Server response code:' . $responseCode . '
'; // echo 'Error description: ' . $responseDecoded['error']['errors'][0]['message']; return $responseDecoded; } else { // echo 'Source: ' . $text . '
'; // echo 'Translation: ' . $responseDecoded['data']['translations'][0]['translatedText']; return $responseDecoded; } Yii::trace($responseDecoded,'lucky'); } }

4.访问api方式

http://localhost:8887/api/bntaketranslateapi?msg=How are you. I am fine&source=en&target=zh

你可能感兴趣的:(Yii的RestApi的使用方法)