這篇文章主要介紹了關於php如何呼叫phantomjs給微信小程式分享的問題,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
背景
CTO不讓我使用golang所以只好用php呼叫phantomjs
packagist上的composer套件功能很冗餘,我只需要用到phantomjs的截圖功能
知識儲備
*unix系統安裝phantomjs,權限相關知識
基本JavaScript語法知識
php
exec
函數呼叫REPL
phantomjsphantomjs js截圖文檔http://javascript.ruanyifeng....
#程式碼(php 程式碼環境為yii2框架)
<?php namespace weapp\library\phantomjs; use weapp\library\BizException; class ScreenShot { /** @var string 获取phantomjs 参数中 js文件的决定路径 */ private $js_path; /** @var bool|string 获取php 有777权限的临时文件目录 */ private $temp_dir; function __construct() { $dir = __DIR__; $this->js_path = "{$dir}/script.js"; /** @var bool|string 获取php 有777权限的临时文件目录 */ $this->temp_dir = \Yii::getAlias('@runtime'); } /** * 截图并上传 * @param string $url * @param string $filename * @return string * @throws BizException */ public function screenShotThenSaveToOss(string $url, string $filename = 'temp.jpg') { //输出图片的路径 $outputFilePath = "{$this->temp_dir}/$filename"; //执行的phantomjs命令 //phantomjs 可执行文件必须是 绝对路径 否则导致 exec 函数返回值127错误 $cmd = "\usr\local\bin\phantomjs {$this->js_path} '$url' '$outputFilePath'"; //捕捉不到phantomjs命令输出结果 exec($cmd, $output); //检查截图文件是否存在 $isShotImgaeExist = file_exists($outputFilePath); if (!$isShotImgaeExist) { throw new BizException(0, 'phantomjs截图失败', BizException::SELF_DEFINE); } //保存截图到oss $result = $this->postScreenShotImageToOss($outputFilePath); //删除临时文件夹的截图图片 unlink($outputFilePath); return $result; } /** * 上传截图到阿里云直传oss * @param string $screenshot_path * @return string */ public function postScreenShotImageToOss(string $screenshot_path): string { $ossKey = 'raw_file_name'; $file = new \CURLFile($screenshot_path, 'image/jpeg', 'file'); $tokenArray = $this->getOssPolicyToken('fetch'); $url = $tokenArray->host; $postData = [ 'key' => "{$tokenArray->dir}/$ossKey", 'policy' => $tokenArray->policy, 'OSSAccessKeyId' => $tokenArray->accessid, 'success_action_status' => '200', 'signature' => $tokenArray->signature, 'callback' => $tokenArray->callback, 'file' => $file ]; $ch = curl_init(); //$data = array('name' => 'Foo', 'file' => '@/home/user/test.png'); curl_setopt($ch, CURLOPT_URL, $url); // Disable SSL verification curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); // required as of PHP 5.6.0 curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 20); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); //curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: $mime_type"]); $res = curl_exec($ch); $res = json_decode($res); curl_close($ch); if (empty($res) || $res->code != 0) { return ''; } else { return $res->data->url; } } /** * 调用管理后台阿里云oss token接口 * @param null $url * @return array */ public function getOssPolicyToken($url = null) { $url = \Yii::$app->params['oss_screen_shot_token_api']; $ch = curl_init(); // Disable SSL verification curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Will return the response, if false it print the response curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Set the url curl_setopt($ch, CURLOPT_URL, $url); // Execute $result = curl_exec($ch); // Closing curl_close($ch); $res = json_decode($result); if (empty($res) || $res->code != 0) { return []; } else { return $res->data; } } }
phantomjs javascript腳本內容
"use strict"; var system = require('system'); var webPage = require('webpage'); var page = webPage.create(); //设置phantomjs的浏览器user-agent page.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'; //获取php exec 函数的命令行参数 if (system.args.length !== 3) { console.log(system.args); console.log('参数错误'); console.log('第2个参数为url地址 第3个参数为截图文件名称'); phantom.exit(1); } //命令行 截图网址参数 var url = system.args[1]; //图片输出路径 var filePath = system.args[2]; console.log('-------'); console.log(url); console.log('-------'); console.log(filePath); console.log('-------'); //设置浏览器视口 page.viewportSize = {width: 480, height: 960}; //打开网址 page.open(url, function start(status) { //1000ms之后开始截图 setTimeout(function () { //截图格式为jpg 80%的图片质量 page.render(filePath, {format: 'jpg', quality: '80'}); console.log('success'); //退出phantomjs 避免phantomjs导致内存泄露 phantom.exit(); }, 1000); });
php呼叫phantomjs目錄架構
以上就是本文的全部內容,希望對大家的學習有幫助,更多相關內容請關注PHP中文網!
相關推薦:
以上是關於php如何呼叫phantomjs給微信小程式分享的問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

要保護應用免受與會話相關的XSS攻擊,需採取以下措施:1.設置HttpOnly和Secure標誌保護會話cookie。 2.對所有用戶輸入進行輸出編碼。 3.實施內容安全策略(CSP)限制腳本來源。通過這些策略,可以有效防護會話相關的XSS攻擊,確保用戶數據安全。

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显著提升应用在高并发环境下的效率。

theSession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceisesneededeededeedeedeededto toavoidperformance andunununununexpectedLogOgouts.3)

在PHP中,可以使用session_name()函數配置會話名稱。具體步驟如下:1.使用session_name()函數設置會話名稱,例如session_name("my_session")。 2.在設置會話名稱後,調用session_start()啟動會話。配置會話名稱可以避免多應用間的會話數據衝突,並增強安全性,但需注意會話名稱的唯一性、安全性、長度和設置時機。

會話ID應在登錄時、敏感操作前和每30分鐘定期重新生成。 1.登錄時重新生成會話ID可防會話固定攻擊。 2.敏感操作前重新生成提高安全性。 3.定期重新生成降低長期利用風險,但需權衡用戶體驗。

在PHP中設置會話cookie參數可以通過session_set_cookie_params()函數實現。 1)使用該函數設置參數,如過期時間、路徑、域名、安全標誌等;2)調用session_start()使參數生效;3)根據需求動態調整參數,如用戶登錄狀態;4)注意設置secure和httponly標誌以提升安全性。

在PHP中使用會話的主要目的是維護用戶在不同頁面之間的狀態。 1)會話通過session_start()函數啟動,創建唯一會話ID並存儲在用戶cookie中。 2)會話數據保存在服務器上,允許在不同請求間傳遞數據,如登錄狀態和購物車內容。

如何在子域名間共享會話?通過設置通用域名的會話cookie實現。 1.在服務器端設置會話cookie的域為.example.com。 2.選擇合適的會話存儲方式,如內存、數據庫或分佈式緩存。 3.通過cookie傳遞會話ID,服務器根據ID檢索和更新會話數據。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Linux新版
SublimeText3 Linux最新版