
首先我们在项目根目录下创建文件,如:
/utils/function.php
文件内容如下:
(相关教程推荐:yii框架)
<?php
function dd($obj)
{
echo "<pre>";
var_dump($obj);
echo "</pre>";
}
function get($url)
{
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$output = curl_exec($ch);
if($error=curl_error($ch)){
return $error;
}
//释放curl句柄
curl_close($ch);
return $output;
}
function post($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}第二步,在web/index.php里面添加下面一句话
require (__DIR__.'/../utils/function.php');
下面我们就可以在全局使用这些方法了。
更多编程相关内容,请关注php中文网编程入门栏目!
以上就是yii框架中工具函数怎么使用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号