[php] php에서 자바스크립트 함수 실행하는 방법 (v8js)
- php 에서 javascript 코드 실행법
* phpv8 / v8js
https://pecl.php.net/package/v8js
https://github.com/phpv8/v8js
- v2.1.1 , 2019/7
- php 7.3까지 지원
- V8 엔진, V8 Javascript Engine library (libv8)
https://github.com/v8/v8
//-------------------------------
* 윈도우에 v8js extension 설치
https://www.apachelounge.com/viewtopic.php?t=6359
- 이 사이트에서 php 7.3 다운로드
v8/v8
The official mirror of the V8 Git repository. Contribute to v8/v8 development by creating an account on GitHub.
github.com
https://windows.php.net/download/
https://phpdev.toolsforresearch.com/php-7.3.16-Win32-VC15-x64.zip
- php의 거의 모든 extesion이 컴파일 되어있다.
- 압축을 푼 root폴더의 5개 dll 파일이 v8js 실행에 필요
icui18n.dll, icuuc.dll , v8.dll , v8_libbase.dll , v8_libplatform.dll
- php.ini 수정
extension=php_v8js.dll
//------------------
예제)
$snapshot = \V8Js::createSnapshot(
'var jsDup = arr => { var arr2 = [...arr, ...arr]; return JSON.stringify(arr2); }'
);
$jscript = new \V8Js('php', array(), array(), true, $snapshot);
echo $jscript->executeString('jsDup([1,2,3])') . "\n";
//------------------------------
* 리눅스에 v8js 설치 방법
https://github.com/phpv8/v8js/blob/php7/README.Linux.md
- v8js extension 을 설치하기 위해서는 v8 library가 필요
- 라이브러리 컴파일이 너무 오래 걸림