Find tags on HTML pages using selectors based on Yii 2 like jQuery
1. The data structure of HTML is as follows, and now it needs to be extracted: video description. . . . , that is, the data in the first p tag
var vmsPlayer_callback = function () {createPlayer("video://vid:166bc1bf143c445a9e6ad0ce0565b517","%3Fguid%3D166bc1bf143c445a9e6ad0ce0565b517%26siteId%3D2","2018-03-13","@WIDTH@","@HEIGHT@")};
视频描述。。。。
2. Search on github: yii2 html dom, and finally choose: garyjl/yii2-simple_html_dom, as shown in Figure 1
3. Execute the command: composer require –prefer-dist garyjl/yii2-simple_html_dom “*”, the installation extension, the installation failed, as shown in Figure 2
But these conflicts with your requirements or minimum-stability.
4. Edit composer.json, add: “garyjl/yii2-simple_html_dom”: “*@dev”, set the support stability tag @dev, as shown in Figure 3
"require": {
"php": ">=5.6.0",
"ext-intl": "*",
"yiisoft/yii2": "^2.0.6",
"yiisoft/yii2-bootstrap": "^2.0.0",
"yiisoft/yii2-swiftmailer": "^2.0.0",
"yiisoft/yii2-authclient": "^2.0.0",
"yiisoft/yii2-jui": "^2.0.0",
"asofter/yii2-imperavi-redactor": "dev-master@dev",
"mihaildev/yii2-elfinder": "^1.0",
"trntv/yii2-aceeditor": "^2.0",
"trntv/probe": "^0.2",
"trntv/yii2-file-kit": "^1.0.0",
"trntv/yii2-glide": "^1.0.0",
"trntv/yii2-datetime-widget": "^1.0.0",
"trntv/cheatsheet": "^0.1@dev",
"trntv/yii2-command-bus": "^2.0",
"intervention/image": "^2.1",
"vlucas/phpdotenv": "^2.0",
"bower-asset/admin-lte": "^2.0",
"bower-asset/font-awesome": "^4.0",
"bower-asset/html5shiv": "^3.0",
"bower-asset/jquery-slimscroll": "^1.3",
"bower-asset/flot": "^0.8",
"symfony/process": "^3.0",
"yiisoft/yii2-redis": "^2.0",
"hprose/hprose-yii": "^2.0",
"garyjl/yii2-simple_html_dom": "*@dev"
},
5. Run the command: composer update, the execution is successful, as shown in Figure 4
6. Content type: video, take out $content[‘content’]the data of the first p tag
use garyjl\simplehtmldom\SimpleHtmlDom;
$html = SimpleHtmlDom::str_get_html($content['content']);
$content = $html->find('p', 0)->text();
print_r($content);
exit;
7. Print the results, in line with expectations, as shown in Figure 5
视频描述。。。。
8. Upgrade to production environment, edit .env, adjust the environment to production environment
YII_DEBUG = false
YII_ENV = prod
9. Run the program, report an error, view:\api\runtime\logs\app.log
2018-03-20 11:04:20 [api][/v1/content-audits?tenantid=default][error][Error] Error: Class 'garyjl\simplehtmldom\SimpleHtmlDom' not found in /sobey/www/cmcp-api/api/models/ContentAudit.php:437
Stack trace:
#0 /sobey/www/cmcp-api/api/rests/content_audit/CreateAction.php(55): api\models\ContentAudit->create()
#1 [internal function]: api\rests\content_audit\CreateAction->run()
#2 /sobey/www/cmcp-api/vendor/yiisoft/yii2/base/Action.php(94): call_user_func_array(Array, Array)
#3 /sobey/www/cmcp-api/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\Action->runWithParams(Array)
#4 /sobey/www/cmcp-api/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('create', Array)
#5 /sobey/www/cmcp-api/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('v1/content-audi...', Array)
#6 /sobey/www/cmcp-api/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#7 /sobey/www/cmcp-api/api/web/index.php(23): yii\base\Application->run()
#8 {main}
2018-03-20 11:04:20 [api][/v1/content-audits?tenantid=default][warning][yii\log\Dispatcher::dispatch] Unable to send log via yii\log\EmailTarget: Exception 'Swift_TransportException' with message 'Expected response code 220 but got code "", with message ""'
in /sobey/www/cmcp-api/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:419
Stack trace:
#0 /sobey/www/cmcp-api/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(325): Swift_Transport_AbstractSmtpTransport->assertResponseCode('', Array)
#1 /sobey/www/cmcp-api/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(130): Swift_Transport_AbstractSmtpTransport->readGreeting()
#2 /sobey/www/cmcp-api/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/SendmailTransport.php(52): Swift_Transport_AbstractSmtpTransport->start()
#3 /sobey/www/cmcp-api/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php(67): Swift_Transport_SendmailTransport->start()
#4 /sobey/www/cmcp-api/vendor/yiisoft/yii2-swiftmailer/Mailer.php(150): Swift_Mailer->send(Object(Swift_Message))
#5 /sobey/www/cmcp-api/vendor/yiisoft/yii2/mail/BaseMailer.php(263): yii\swiftmailer\Mailer->sendMessage(Object(yii\swiftmailer\Message))
#6 /sobey/www/cmcp-api/vendor/yiisoft/yii2/mail/BaseMessage.php(49): yii\mail\BaseMailer->send(Object(yii\swiftmailer\Message))
#7 /sobey/www/cmcp-api/vendor/yiisoft/yii2/log/EmailTarget.php(88): yii\mail\BaseMessage->send(Object(yii\swiftmailer\Mailer))
#8 /sobey/www/cmcp-api/vendor/yiisoft/yii2/log/Target.php(133): yii\log\EmailTarget->export()
#9 /sobey/www/cmcp-api/vendor/yiisoft/yii2/log/Dispatcher.php(189): yii\log\Target->collect(Array, true)
#10 /sobey/www/cmcp-api/vendor/yiisoft/yii2/log/Logger.php(177): yii\log\Dispatcher->dispatch(Array, true)
#11 /sobey/www/cmcp-api/vendor/yiisoft/yii2/base/ErrorHandler.php(113): yii\log\Logger->flush(true)
#12 [internal function]: yii\base\ErrorHandler->handleException(Object(Error))
#13 {main}
10. Check git and find that the \vendor\garyjl\yii2-simple_html_dom directory is empty, as shown in Figure 6
11. Delete and add to the ignore list:\vendor\garyjl\yii2-simple_html_dom, as shown in Figure 7
12. Edit .gitignore, delete a line: /vendor/garyjl/yii2-simple_html_dom, save, as shown in Figure 8
13. Delete the directory:\vendor\garyjl\yii2-simple_html_dom\.git, as shown in Figure 9
14. Added: /vendor/garyjl/yii2-simple_html_dom, as shown in Figure 10
15. Check \common\config\base.php
$config = [
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
//'useFileTransport' => true,
'messageConfig' => [
'charset' => 'UTF-8',
'from' => env('ADMIN_EMAIL')
]
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
'file'=>[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
'except' => ['yii\web\HttpException:*', 'yii\i18n\I18N\*'],
'prefix' => function () {
$url = !Yii::$app->request->isConsoleRequest ? Yii::$app->request->getUrl() : null;
return sprintf('[%s][%s]', Yii::$app->id, $url);
},
'logVars'=>[],
],
'httpRequest'=>[
'class' => 'yii\log\FileTarget',
'logFile' => '@runtime/logs/http-request.log',
'categories' => ['yii\httpclient\*'],
]
],
],
],
];
if (YII_ENV_PROD) {
$config['components']['log']['targets']['email'] = [
'class' => 'yii\log\EmailTarget',
'except' => ['yii\web\HttpException:*'],
'levels' => ['error', 'warning'],
'message' => ['from' => env('ROBOT_EMAIL'), 'to' => env('ADMIN_EMAIL')]
];
}
16. Comment log target:[[yii\log\EmailTarget|email target]], edit \common\config\base.php
/*
if (YII_ENV_PROD) {
$config['components']['log']['targets']['email'] = [
'class' => 'yii\log\EmailTarget',
'except' => ['yii\web\HttpException:*'],
'levels' => ['error', 'warning'],
'message' => ['from' => env('ROBOT_EMAIL'), 'to' => env('ADMIN_EMAIL')]
];
}
*/









