php接口

简介:

入口

复制代码
<?php

class Api_IphoneController extends actions_api {

    var $identity = null;

    function init() {
        parent::init();
        $this->setView('api');
    }

    function indexAction() {
        $json = $_REQUEST;
        //print_r(json_decode($json['json'],true));exit;
        if (array_key_exists('json', $json)) {
            $json_info = json_decode($json['json'], true);
            //$this->debuglog($json['json']); // debug
        } else {
            echo "无请求参数,请输入请求参数!";
            die();
        }
        //通过reqCode转入控制层
        switch ($json_info["reqCode"]) {
            case 'edu00001':
                // 获取新闻,分页获取
                // 请求{"reqCode":"edu00001","data":{"nowpage":"1","pageNum":"10","user_id":"10","bigclassid":"221"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('newslist', 'news', 'api', $json_info);
                break;

            case 'edu00002':
                // 获取所有老师
                // 请求{"reqCode":"edu00002"}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('allteacher', 'teacher', 'api', $json_info);
                break;

            case 'edu00003':
                // 新闻top5
                // 请求{"reqCode":"edu00003","data":{"user_id":"105"}}
                // 响应 
                $json_info = json_decode($json['json'], true);
                $this->_forward('newstop5', 'news', 'api', $json_info);
                break;


            case 'edu00004':
                // 获取收件箱消息,分页获取
                // 请求{"reqCode":"edu00004","data":{"user_id":"105","nowpage":"1","pageNum":"10"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('msginlist', 'message', 'api', $json_info);
                break;

            case 'edu00005':
                // 所有班级
                // 请求{"reqCode":"edu00005"}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('allclassinfo', 'student', 'api', $json_info);
                break;

            case 'edu00006':
                // 登录
                // 请求{"reqCode":"edu00006","data":{"username":"test3","password":"123456"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('login', 'auth', 'api', $json_info);
                break;

            case 'edu00007':
                // 班级信息
                // 请求{"reqCode":"edu00007","data":{"class_id":"19"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('classinfo', 'student', 'api', $json_info);
                break;

            case 'edu00008':
                // 新闻分类
                // 请求{"reqCode":"edu00008"}
                // 响应
                $json_info = json_decode($json['json'], true);

                $this->_forward('newstype', 'news', 'api', $json_info);
                break;

            case 'edu00009':
                // 学生信息
                // 请求{"reqCode":"edu00009","data":{"user_id":"166"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('studentinfo', 'student', 'api', $json_info);
                break;

            case 'edu00010':
                // 消息发送
                // 请求{"reqCode":"edu00010","data":{"from_id":"21","to_id":"105,22,23","topic":"消息主题","content":"消息内容"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('msgsend', 'message', 'api', $json_info);
                break;
            default:
                echo '请求代码错误!!';
        }
    }

}
复制代码

处理

复制代码
<?php
class Api_NewsController extends actions_api {
    public $doMain = 'http://testserver.njlrxx.com/';


    //接口中可以直接操作sql语句进行一些处理
    function init() {
        parent::init();
        $this->setView('api');
        $this->dao_newsrecord = new dao_newsrecord();
        $this->dao_module = new dao_module();
        $this->dao_message = new dao_message();
        $this->dao_user = new dao_user();
        $this->dao_news = new dao_news();
        $this->inData = $this->_getParam('data',false);//请求参数
        $this->reqCode = $this->_getParam('reqCode',false);
        $this->outData = array('status'=>0,'msg'=>'','data'=>array(),'reqCode'=>$this->reqCode);//输出参数
    }
    

    //新闻分类
    function newstypeAction()
    {
        $where['BigClass.typeid = ?'] = array("type"=>1,"val"=>60);
        $aBigClass = $this->dao_news->getBigClass($where, 'BigClassID ASC', 3);
        if(COUNT($aBigClass) == 0)
        {
            $this->outData['msg'] = '暂无数据';
            $this->printOut();
        }
        $this->outData['status'] = 1;
        $this->outData['msg'] = '获取成功';
        $this->outData['data'] = $aBigClass;
        $this->printOut();
    }


    //图片新闻前五条
    function newstop5Action(){
        $where['News.picnews = ?'] = array("type"=>1,"val"=>1);
        $aNewsData = $this->dao_news->getNews2($where, 'NewsID DESC', 5, FALSE , false, array('NewsID','picname','Title'));
        foreach($aNewsData as &$val)
        {
            $val['webview_url'] = $this->doMain.'api/news/newsview/NewsID/'.$val['NewsID'].'/user_id/'.$this->inData['user_id'];
        }
        if(COUNT($aNewsData) == 0)
        {
            $this->outData['msg'] = '暂无数据';
            $this->printOut();
        }
        $this->outData['status'] = 1;
        $this->outData['msg'] = '获取成功';
        $this->outData['data'] = $aNewsData;
        $this->printOut();
    }

    function newslistAction(){
        $nowPage = !empty($this->inData['nowpage'])?$this->inData['nowpage']:1;    //$this->_getParam('nowpage',1);
        $pageNum = !empty($this->inData['pageNum'])?$this->inData['pageNum']:10;    //$this->_getParam('pageNum',10);
        $bigclassid = !empty($this->inData['bigclassid'])?$this->inData['bigclassid']:false;    
         if(!$bigclassid)
        {
            $this->outData['msg'] = '缺少分类参数';
            $this->printOut();
        }
        $offset = ($nowPage - 1)*$pageNum;
        $where['News.bigclassid = ?'] = array("type"=>1,"val"=>$bigclassid);
        $total = $this->dao_news->getNews2($where, '', false, false , true);
        $aNewsData = $this->dao_news->getNews2($where, 'NewsID DESC', $pageNum, $offset , false, array('NewsID','Title','UpdateTime','convert(text,Content) as Content'));

        foreach($aNewsData as &$val)
        {
            $val['webview_url'] = $this->doMain.'api/news/newsview/NewsID/'.$val['NewsID'].'/user_id/'.$this->inData['user_id'];
            $val['Content'] = $this->substring($this->clearhtml($val["Content"]),16);
        }
        if($total == 0)
        {
            $this->outData['msg'] = '暂无数据';
            $this->printOut();
        }
        $this->outData['next'] = 0;
        if($total > ($nowPage * $pageNum))
        {
            $this->outData['next'] = 1;
        }
        $this->outData['status'] = 1;
        $this->outData['msg'] = '获取成功';
        $this->outData['data'] = $aNewsData;

        $this->printOut();
    }

    function newsviewAction(){
        $newsID = $this->_getParam("NewsID",0);//新闻id
        $userID = $this->_getParam("user_id");//uid

        //$whereUser['lx_user.id = ?'] = array("type"=>1,"val"=>$user_id);
        //$aUser = $this->dao_user->getUser($whereUser);
    
        $whereNews['News.NewsID = ?'] = array("type"=>1,"val"=>$newsID);
        $aNews = $this->dao_news->getNews($whereNews);

        $aNewsData = $aNews ? $aNews[0] : false;
        if(!$aNewsData && $aNewsData['checkked'] == 1 && $userID)//是否审核
        {
            $this->dao_news->updateNews(array('click'=>'`click`+1','NewsID'=>$newsID));//更新新闻点击次数
             //阅读人记录到数据库中
            $aNewsrecord = array();
            $aNewsrecord['news_id'] = $NewsID;
            $aNewsrecord['user_id'] = $userID;
            $aNewsrecord['user_name'] = !is_null($aUser[0]["name"]) ? $aUser[0]["name"] : $aUser[0]["surname"].$aUser[0]["givenname"];
            $aNewsrecord['user_avatar'] = $aUser[0]["avatar"];
            $aNewsrecord['time'] = time();
            $whereNewsrecord = array();
            $whereNewsrecord['lx_newsrecord.news_id = ?'] = array("type"=>1,"val"=>$NewsID);
            $whereNewsrecord['lx_newsrecord.user_id = ?'] = array("type"=>1,"val"=>$userID);
            $result = $this->dao_newsrecord->getNewsrecord($whereNewsrecord);
            if($result){
                $aNewsrecord['id'] = $result[0]['id'];
                $this->dao_newsrecord->updateNewsrecord($aNewsrecord);
            }else{
                $this->dao_newsrecord->addNewsrecord($aNewsrecord);
            }
        }
        $aNewsData['Content'] =  str_replace('src="/webedit/uploadfile/','src="http://www.njlrxx.com/webedit/uploadfile/',$aNewsData['Content']);

        $this->view_->assign('aNewsData', $aNewsData);
        $this->view_->assign('userID', $userID);
        $this->view_->display('news_view_iphone.tpl');
    }
}


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/3557892.html,如需转载请自行联系原作者

相关文章
|
5月前
|
Java PHP 数据安全/隐私保护
PHP 面向对象,构造函数,析构函数,继承,方法的重写,接口抽象类,static,final,this,parent,self的异同和作用
本文详细介绍了PHP面向对象编程的一系列核心概念和用法,包括构造函数、析构函数、继承、方法重写、访问控制、接口、抽象类、静态成员、final关键字、以及this、self、parent这三个关键字的异同和作用。通过具体示例代码,展示了如何在PHP中使用这些面向对象的特性,以及它们在实际开发中的应用。
PHP 面向对象,构造函数,析构函数,继承,方法的重写,接口抽象类,static,final,this,parent,self的异同和作用
|
6月前
|
网络协议 API PHP
PhalApi:在宝塔一键安装部署PHP开源接口框架的教程
要在宝塔面板上一键安装部署PhalApi开源接口框架,首先进入宝塔软件商店,切换到“一键部署”选项,搜索“phalapi”并点击“一键部署”。安装时需填写接口域名、数据库名及密码,提交后等待安装完成。安装成功后可在宝塔面板中查看新站点和源代码目录,并通过DNS解析设置访问接口域名,如`http://myapi.phalapi.net/`。默认开启的调试模式便于测试,可通过修改`config/sys.php`中的`debug`值为`false`关闭。最后,在源代码中开发自己的PHP接口,PhalApi会自动生成在线接口文档,方便后续调用与维护。更多详细教程可参考官方文档。
|
7月前
|
PHP
PHP 接口和继承的异同?
【7月更文挑战第2天】PHP 接口和继承的异同?
40 0
|
7月前
|
PHP
PHP中接口如何定义?
【7月更文挑战第2天】PHP中接口如何定义?
44 0
|
7月前
|
前端开发 PHP 数据格式
【附带效果视频】php接口给前端返回流式数据,php使用event-stream进行数据推送,循环一次输出一次
【附带效果视频】php接口给前端返回流式数据,php使用event-stream进行数据推送,循环一次输出一次
265 0
|
7月前
|
Java API PHP
【亲测有效,官方提供】php版本企查查api接口请求示例代码,php请求企查查api接口,thinkphp请求企查查api接口
【亲测有效,官方提供】php版本企查查api接口请求示例代码,php请求企查查api接口,thinkphp请求企查查api接口
238 1
|
8月前
|
JSON 安全 API
实战指南:使用PHP构建高性能API接口服务端
构建RESTful API的简要指南:使用PHP和Laravel,先安装Laravel并配置数据库,接着在`api.php`中定义资源路由,创建`PostController`处理CRUD操作,定义`Post`模型与数据库交互。使用Postman测试API功能,如创建文章。别忘了关注安全性、错误处理和性能优化。
197 2
|
9月前
|
监控 PHP Python
1688快速获取整店铺列表 采集接口php Python
在电子商务的浪潮中,1688平台作为中国领先的批发交易平台,为广大商家提供了一个展示和销售商品的广阔舞台;然而,要在众多店铺中脱颖而出,快速获取商品列表并进行有效营销是关键。
|
9月前
|
PHP 数据安全/隐私保护
【PHP开发专栏】PHP接口与抽象类的应用
【4月更文挑战第30天】本文探讨了PHP中接口与抽象类的使用,包括定义、实现和比较。接口用于规定实现类必须提供的方法签名,而抽象类则可以包含方法实现和抽象方法。一个类可实现多个接口,但只能继承一个抽象类。根据需求,若需定义不相关类的共同方法,选择接口;若需提供共享属性和非抽象方法,选择抽象类。通过实战应用示例,展示了如何在动物园管理系统中结合接口和抽象类进行设计。理解两者有助于提升代码的复用性和可维护性。
53 2
|
9月前
|
XML JSON API
快速淘宝商品详情页面API接口传输 php
PI(Application Programming Interface,应用程序接口)是一组预定义的函数、协议和工具,用于构建软件应用程序之间的交互。它允许不同的软件系统和应用通过统一的接口进行数据交换和通信

热门文章

最新文章