[PHP] node.js를 윈도우 서버에 설치하고 peppeteer 라이브러리를 이용하여 og:image를 추출해 보자
페이지 정보
shadow2fox
367 1 2
2025.06.13 19:52:21
짧은주소
-
https://yadolee.com/tip/1135 주소복사
본문
📝 node.js를 윈도우 서버에 설치하고 peppeteer 라이브러리를 이용하여 og:image를 추출해 보자
일반적으로 curl로 HTML 메타 태그를 파싱할 수 있으나 Meta 플랫폼 서비스(페이스북, 스레드 등)는 대부분의 콘텐츠를 로그인 사용자 기반의 클라이언트 렌더링으로 제공해서 curl이나 일반 HTTP 요청으로는 HTML 메타 태그를 추출할 수 없다.
1. Node.js 설치
https://nodejs.org/ko 접속하여 LTS 버전 다운로드 후 Node.js를 설치한다.
설정 마지막 단계 tool for Native Modules의 체크 박스를 선택하지 않고 설치한다.
2. Puppeteer 설치
cmd 창이나 터미널 창을 열어 아래 명령어를 입력한다.
mkdir C:\puppeteer
cd C:\puppeteer
npm init -y
npm install puppeteer
3. ogimage.js 작성
puppeteer가 설치된 폴더에 아래 소스 코드로 ogimage.js 파일을 작성한다.
const puppeteer = require('puppeteer');
(async () => {
const url = process.argv[2];
if (!url || !url.startsWith('http')) {
process.exit(0);
}
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
// 일반적인 브라우저처럼 가장 널리 호환되는 User-Agent 설정
await page.setUserAgent(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
);
try {
await page.goto(url, { waitUntil: 'networkidle2', timeout: 15000 });
// og:image만 추출
const ogImage = await page.evaluate(() => {
const meta = document.querySelector('meta[property="og:image"]');
return meta ? meta.content : '';
});
console.log(ogImage || '');
} catch (err) {
console.log('');
} finally {
await browser.close();
}
})();
4. PHP에서 활용
$node = 'C:\\Program Files\\nodejs\\node.exe';
$script = 'C:\\puppeteer\\ogimage.js';
$url = '이미지를 추출할 url 주소';
$command = "\"$node\" \"$script\" \"$url\"";
exec($command, $output, $result_code);
if ($result_code === 0 && !empty($output))
$og_image = trim(implode("\n", $output));
$url은 preg_match를 활용해서 추출한다.
추출한 $og_image의 이미지 주소를 이용해 서버에 다운로드 하고 가공해 활용하도록 한다.
추천인 2
레벨 148
경험치 3,296,614
Progress Bar 74.81%
- 가입일 : 2015-03-10 12:21:44
- 서명 : 인간에게 가장 큰 선물은
자기 자신에게 기회를 주는 것이다.
- 크리스 가드너, Chris Gardner -
- 자기소개 : There's never a shortcut to happiness.


댓글1
하늘구장님의 댓글
윈도우 서버 이용자라서 저도 한번 도전해 보고 싶네요
멋진 팁 고맙습니다
축하합니다. 첫 댓글로 29경험치를 획득하였습니다.