thinkphp使用自定义类方法
2024-10-10 12:57:56
1.通过Model调用
<?php
/**
* 积分模型 api接口
*/
class ApiModel{
private $url = 'http://js.yunlutong.com/Customer/Interface';
public function test() {
$post_data['action'] = 'sadf';
$post_data['callback'] = '?';
$res = request_post($this->url, $post_data);
$firstChar = substr($res,0,1);
if ($firstChar =='?') {
$res = substr($res,2);
$res = substr($res,0,strlen($res)-1);
} elseif($firstChar == '(') {
$res = substr($res,1);
$res = substr($res,0,strlen($res)-1);
}
dump(json_decode($res,true));
}
}
没有继承Model,否则会因为表不存在而报错。
调用,
$Api = D('Api');
$Api->test();
调用确实方便,但是总感觉有点不合理。这个D毕竟是操作数据库的。
2.通过引入类实现,把类放到ORG下
<?php
class Integral{
private $url = 'http://js.yunlutong.com/Customer/Interface';
public function test() {
$post_data['action'] = 'sadf';
$post_data['callback'] = '?';
$res = request_post($this->url, $post_data);
$firstChar = substr($res,0,1);
if ($firstChar =='?') {
$res = substr($res,2);
$res = substr($res,0,strlen($res)-1);
} elseif($firstChar == '(') {
$res = substr($res,1);
$res = substr($res,0,strlen($res)-1);
}
dump($res);
dump(json_decode($res,true));
}
}
?>
调用
import("@.ORG.Api.Integral");
$integralApi = new Integral();
$integralApi->test();
配置一下,自动加载
'APP_AUTOLOAD_PATH' => '@.ORG,@.ORG.Api',
这样调用就方便了不管Api文件夹下有多少类,都会自动加载,不需要单个引用import("@.ORG.Api.Integral")了。
最新文章
- DELPHI实现关闭指定进程,自身防杀
- Spark:读取hdfs gz压缩包
- Spring透过ApplicationListener来触发contextrefreshedevent事件
- 如何查看mac系统是32位还是64位的操作系统
- C字符串和C++中string的区别 &;&;&;&;C++中int型与string型互相转换
- centos nginx和tomcat 通过反向代理生成想要的nexus网址
- Linux kernel ‘aac_send_raw_srb’函数输入验证漏洞
- Linux OpenCV读取视频失败,cvCreateFileCapture失败的解决
- JavaScript详解
- python爬虫入门(八)Scrapy框架之CrawlSpider类
- MySql 8.0 C#连接报错 MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host &#39;12.118.224.181&#39; for user &#39;root&#39; using method &#39;caching_sha2_password&#39; failed with message: Reading from t
- 使用OClint进行iOS项目的静态代码扫描
- boost asio 一个聊天的基本框架
- 在SpringTest中将Mockito的mock对象通过spring注入使用
- FFT(快速傅里叶变换)算法详解
- just_sort
- windows10下如何进行源码编译安装tensorflow
- 分享 - 普通程序员如何转向AI方向
- ted飞行器
- JAVA变量的执行顺序
热门文章
- php变量简单介绍
- 报错org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;sessionFactory&#39; defined in class path resource [bean.xml]
- 用 LoadLibraryExW 函数测试加载 dll (CSharp、Windows)
- Linux 下硬链接和软链接的说明
- MongoCola使用教程 2 - MongoDB的Replset 初始化和配置
- 再论typedef
- 记录下httpclient 发送请求 服务端用@RequestBody 自动接收参数 报415
- Android中轻松使用线程
- .Net 中的IL中间语言基本语法
- Linux下shell命令 1