First we create a file in the project root directory, such as:
/utils/function.php
The content of the file is as follows:
(Related tutorials recommended: yii framework)
<?php function dd($obj) { echo "<pre class="brush:php;toolbar:false">"; var_dump($obj); echo ""; } 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; }
The second step is to add the following sentence in web/index.php
require (__DIR__.'/../utils/function.php');
Below we can Use these methods globally.
For more programming related content, please pay attention to the Programming Introduction column on the php Chinese website!
The above is the detailed content of How to use tool functions in yii framework. For more information, please follow other related articles on the PHP Chinese website!