长春子
- 长春子 回复了话题 25款实用网站后台模板
在哪里下载呢 碰到这样的楼主也是醉了。
- 长春子 2015-06-02 已签到连续签到2天,获得了10个金钱
- 长春子 2015-06-01 已签到连续签到1天,获得了5个金钱
- 长春子 赞了回答
1 api接口本来就不是给浏览器用的。
2 实在要跟浏览器扯上关系的话,可以安装扩展,比如postman,注意不要太相信扩展程序,比如postman对update和delete貌似就有bug。
3 建议用curl命令直接测试。api消费者的开发也没浏览器什么事,比如安卓用HttpClient、HttpConnection或者直接用socket写客户端。
- 长春子 赞了回答
- 长春子 赞了回答
提供以下几种方法:
1)如果题主是用PhpStorm来开发项目的话,里面有个Rest Client插件,用来测试Restful接口十分方便,支持Get/Post/Delete/Put等Http method,还支持上传文件,修改Http header;
2)用curl命令测试;
3)用浏览器测试,不过在测试非Get方式访问或需要自定义Http header的API时,需要安装插件;
4)自己写一个HTML网页,里面嵌入一个form表单,同样可以做测试,只是稍微麻烦了点。 水平不行,不过功能实现了。 将就看,如果有更好的实现方法,更安全的代码,还请分享。
<?php /* * REST文件上传 * * * @项目 com.xxx.www * @作者 Mx * @编码 UTF-8 * @日期 2015-5-16 16:41:49 */ namespace api\controllers; use yii; /** * 类名称: uploadcontroller * 类说明: */ class UploadController extends \yii\rest\ActiveController { public $documentPath = 'documents/';//上传路径 public $modelClass = 'common\models\xxx';//我不会不继承\yii\rest\ActiveController的方式实现这里的方法,所以给了一个没有意义的model,不过下方通过 注销自带方法 去除了 本REST中队 xxx的功能,所以除非yii2自身漏洞,否则对于xxx不会有问题 public function actions() { $actions = parent::actions(); // 注销系统自带的实现方法 unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']); return $actions; } public function actionUpdate() { $postdata = fopen($_FILES['data']['tmp_name'], "r"); /* Get file extension */ $extension = substr($_FILES['data']['name'], strrpos($_FILES['data']['name'], '.')); /* Generate unique name */ $filename = $this->documentPath . uniqid() . $extension; /* Open a file for writing */ $fp = fopen($filename, "w"); /* Read the data 1 KB at a time and write to the file */ while ($data = fread($postdata, 1024)) fwrite($fp, $data); /* Close the streams */ fclose($fp); fclose($postdata); /* the result object that is sent to client */ $result['filename'] = $filename; $result['document'] = $_FILES['data']['name']; $result['create_time'] = date("Y-m-d H:i:s"); return $result; } }
一会测试一下 ,辛苦了。