yii框架中工具函數(shù)怎么使用

yii框架中工具函數(shù)怎么使用

首先我們在項目根目錄下創(chuàng)建文件,如:

/utils/function.php

文件內(nèi)容如下:

(相關(guān)教程推薦:yii框架

<?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); //執(zhí)行并獲取HTML文檔內(nèi)容 $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數(shù)據(jù) 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');

下面我們就可以在全局使用這些方法了。

更多編程相關(guān)內(nèi)容,請關(guān)注php中文網(wǎng)yii框架欄目!

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊8 分享