菜单

Yii2中OAuth扩展及QQ互联登录实现方法_php实例_脚本之家澳门太阳集团

2020年1月31日 - 首页

正文实例陈述了Yii第22中学OAuth扩充及QQ互联登入完成方式。分享给大家供大家参谋,具体如下:复制代码 代码如下:php composer.phar require
–prefer-dist yiisoft/yii2-authclient “*”

正文实例叙述了Yii框架使用PHPExcel导出Excel文件的主意。共享给大家供大家参照他事他说加以考查,具体如下:

澳门太阳集团,Quick start 快速起初

这几天在商讨PHP的Yii框架,很爱怜,境遇导出Excel的主题素材,商讨了须臾间,就有了上面包车型大巴点子:

纠正Yii2的结构文件config/main.php,在components中加进如下内容

1、首先在configmain.php中增添对PHPExcel的引用,我的方法是如此:

'components' => [ 'authClientCollection' => [ 'class' => 'yiiauthclientCollection', 'clients' => [ 'google' => [ 'class' => 'yiiauthclientclientsGoogleOpenId' ], 'facebook' => [ 'class' => 'yiiauthclientclientsFacebook', 'clientId' => 'facebook_client_id', 'clientSecret' => 'facebook_client_secret', ], ], ] ...]
// autoloading model and component classes'import'=>array( /*'application.modules.srbac.controllers.SBaseController',*/ 'application.models.*', 'application.components.*', 'application.extensions.phpexcel.*',),

校勘入口文件,平日是app/controllers/SiteController.php,在function
actions扩张代码,同一时候扩大回调函数successCallback,大约如下

别的也可能有人用components 那一个构造,可是本身的有标题,所以就用地点的方法。

class SiteController extends Controller{ public function actions() { return [ 'auth' => [ 'class' => 'yiiauthclientAuthAction', 'successCallback' => [$this, 'successCallback'], ], ] } public function successCallback { $attributes = $client->getUserAttributes(); // user login or signup comes here }}

2、依照上面包车型大巴代码改过PHPExcel代码目录里的Autoloader.php文件:

在登录的Views中,增添如下代码

public static function Register() { /*if (function_exists { // Register any existing autoloader function with SPL, so we don't get any clashes spl_autoload_register; } // Register ourselves with SPL return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'));*/ $functions = spl_autoload_functions(); foreach ( $functions as $function) spl_autoload_unregister; $functions = array_merge(array(array('PHPExcel_Autoloader','Load')),$functions); foreach ( $functions as $function) $x = spl_autoload_register; return $x;} // function Register()
 ['site/auth']])?>

上面的函数中,注释掉的是本来的代码。

以上是合法的求证文书档案,下边大家来衔接QQ互联

3、上面包车型地铁代码是输出Excel,以致一些常用的性质设置,在您的controller中:

日增QQ登入的零部件 作者那边是身处 common/components/QqOAuth.php
中,源代码如下

/*导出为Excel*/public function actionExport(){ $objectPHPExcel = new PHPExcel(); $objectPHPExcel->setActiveSheetIndex; $page_size = 52; //数据的取出 $model = Yii::app()->session['printdata']; $dataProvider = $model->search(); $dataProvider->setPagination; $data = $dataProvider->getData(); $count = $dataProvider->getTotalItemCount(); //总页数的算出 $page_count =  +1; $current_page = 0; $n = 0; foreach  { if ( $n % $page_size === 0 ) { $current_page = $current_page +1; //报表头的输出 $objectPHPExcel->getActiveSheet()->mergeCells; $objectPHPExcel->getActiveSheet()->setCellValue; $objectPHPExcel->setActiveSheetIndex->setCellValue; $objectPHPExcel->setActiveSheetIndex->setCellValue; $objectPHPExcel->setActiveSheetIndex->getFont; $objectPHPExcel->setActiveSheetIndex ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objectPHPExcel->setActiveSheetIndex->setCellValue('B2','日期:'.date; $objectPHPExcel->setActiveSheetIndex->setCellValue('G2','第'.$current_page.'/'.$page_count.'页'); $objectPHPExcel->setActiveSheetIndex ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); //表格头的输出 $objectPHPExcel->getActiveSheet()->getColumnDimension; $objectPHPExcel->setActiveSheetIndex->setCellValue; $objectPHPExcel->getActiveSheet()->getColumnDimension; $objectPHPExcel->setActiveSheetIndex->setCellValue; $objectPHPExcel->getActiveSheet()->getColumnDimension; $objectPHPExcel->setActiveSheetIndex->setCellValue; $objectPHPExcel->getActiveSheet()->getColumnDimension; $objectPHPExcel->setActiveSheetIndex->setCellValue; $objectPHPExcel->getActiveSheet()->getColumnDimension; $objectPHPExcel->setActiveSheetIndex->setCellValue; $objectPHPExcel->getActiveSheet()->getColumnDimension; $objectPHPExcel->setActiveSheetIndex->setCellValue; $objectPHPExcel->getActiveSheet()->getColumnDimension; //设置居中 $objectPHPExcel->getActiveSheet ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //设置边框 $objectPHPExcel->getActiveSheet ->getBorders->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel->getActiveSheet ->getBorders->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel->getActiveSheet ->getBorders->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel->getActiveSheet ->getBorders->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel->getActiveSheet ->getBorders->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); //设置颜色 $objectPHPExcel->getActiveSheet->getFill() ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB; } //明细的输出 $objectPHPExcel->getActiveSheet()->setCellValue ,$product->id); $objectPHPExcel->getActiveSheet()->setCellValue ,$product->product_name); $objectPHPExcel->getActiveSheet()->setCellValue ,$product->product_agent->name); $objectPHPExcel->getActiveSheet()->setCellValue ,$product->unit); $objectPHPExcel->getActiveSheet()->setCellValue ,$product->unit_price); $objectPHPExcel->getActiveSheet()->setCellValue ,$product->library_count); //设置边框 $currentRowNum = $n+4; $objectPHPExcel->getActiveSheet()->getStyle.':G'.$currentRowNum ) ->getBorders->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel->getActiveSheet()->getStyle.':G'.$currentRowNum ) ->getBorders->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel->getActiveSheet()->getStyle.':G'.$currentRowNum ) ->getBorders->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel->getActiveSheet()->getStyle.':G'.$currentRowNum ) ->getBorders->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel->getActiveSheet()->getStyle.':G'.$currentRowNum ) ->getBorders->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $n = $n +1; } //设置分页显示 //$objectPHPExcel->getActiveSheet()->setBreak( 'I55' , PHPExcel_Worksheet::BREAK_ROW ); //$objectPHPExcel->getActiveSheet()->setBreak( 'I10' , PHPExcel_Worksheet::BREAK_COLUMN ); $objectPHPExcel->getActiveSheet->setHorizontalCentered; $objectPHPExcel->getActiveSheet->setVerticalCentered; ob_end_clean; header('Content-Type : application/vnd.ms-excel'); header('Content-Disposition:attachment;filename="'.'产品信息表-'.date; $objWriter= PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5'); $objWriter->save;}
 [ * 'authClientCollection' => [ * 'class' => 'yiiauthclientCollection', * 'clients' => [ * 'qq' => [ * 'class' => 'commoncomponentsQqOAuth', * 'clientId' => 'qq_client_id', * 'clientSecret' => 'qq_client_secret', * ], * ], * ] * ... * ] * ~~~ * * @see http://connect.qq.com/ * * @author easypao  * @since 2.0 */class QqOAuth extends OAuth2{ public $authUrl = 'https://graph.qq.com/oauth2.0/authorize'; public $tokenUrl = 'https://graph.qq.com/oauth2.0/token'; public $apiBaseUrl = 'https://graph.qq.com'; public function init; if ($this->scope === null) { $this->scope = implode(',', [ 'get_user_info', ]); } } protected function initUserAttributes() { $openid = $this->api; $qquser = $this->api("user/get_user_info", 'GET', ['oauth_consumer_key'=>$openid['client_id'], 'openid'=>$openid['openid']]); $qquser['openid']=$openid['openid']; return $qquser; } protected function defaultName() { return 'qq'; } protected function defaultTitle() { return 'Qq'; } /** * 该扩展初始的处理方法似乎QQ互联不能用,应此改写了方法 * @see yiiauthclientBaseOAuth::processResponse() */ protected function processResponse($rawResponse, $contentType = self::CONTENT_TYPE_AUTO) { if  { return []; } switch  { case self::CONTENT_TYPE_AUTO: { $contentType = $this->determineContentTypeByRaw; if ($contentType == self::CONTENT_TYPE_AUTO) { //以下代码是特别针对QQ互联登录的,也是与原方法不一样的地方 if(strpos($rawResponse, "callback") !== false){ $lpos = strpos; $rpos = strrpos; $rawResponse = substr($rawResponse, $lpos + 1, $rpos - $lpos -1); $response = $this->processResponse($rawResponse, self::CONTENT_TYPE_JSON); break; } //代码添加结束 throw new Exception('Unable to determine response content type automatically.'); } $response = $this->processResponse($rawResponse, $contentType); break; } case self::CONTENT_TYPE_JSON: { $response = Json::decode; if (isset { throw new Exception('Response error: ' . $response['error']); } break; } case self::CONTENT_TYPE_URLENCODED: { $response = []; parse_str($rawResponse, $response); break; } case self::CONTENT_TYPE_XML: { $response = $this->convertXmlToArray; break; } default: { throw new Exception('Unknown response type "' . $contentType . '".'); } } return $response; }}

代码推行后,会平昔生成Excel,并提醒下载或张开。

更正 config/main.php 文件,在components中加进,大概如下

更加多关于Yii相关内容感兴趣的读者可查阅本站专项论题:《Yii框架入门及常用本事总括》、《php特出开辟框架总计》、《smarty模板入门功底教程》、《php面向对象程序设计入门教程》、《php字符串用法总计》、《php+mysql数据库操作入门教程》及《php置身事外数据库操作技巧汇总》

'components' => [ 'authClientCollection' => [ 'class' => 'yiiauthclientCollection', 'clients' => [ 'qq' => [ 'class'=>'commoncomponentsQqOAuth', 'clientId'=>'your_qq_clientid', 'clientSecret'=>'your_qq_secret' ], ], ]]

梦想本文所述对大家基于Yii框架的PHP程序设计具备助于。

SiteController.php 就按法定那样子

public function successCallback{ $attributes = $client->getUserAttributes(); // 用户的信息在$attributes中,以下是您根据您的实际情况增加的代码 // 如果您同时有QQ互联登录,新浪微博等,可以通过 $client->id 来区别。}

末段在登入的视图像和文字件中 扩充QQ登录链接

使用QQ快速登录

PS:我在这里间推荐生机勃勃款本站的php格式化美化的排版工具扶持大家在未来的PHP程序设计中实行代码制版:php代码在线格式化美化学工业具:

越多关于Yii相关内容感兴趣的读者可查阅本站专项论题:《Yii框架入门及常用技术总结》、《php卓绝开采框架总括》、《smarty模板入门底子教程》、《php日期与时光用法计算》、《php面向对象程序设计入门教程》、《php字符串用法总括》、《php+mysql数据库操作入门教程》及《php听而不闻数据库操作技艺汇总》

但愿本文所述对大家基于Yii框架的PHP程序设计具备利于。

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图