사이트 정보(파비콘, 제목, 설명) 가져오는 함수
[code]
function fetchWebsiteMetadata($url) {
// "http://"가 생략된 경우 처리
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
$ch = curl_init();
// curl 옵션 설정
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
// curl 실행 및 에러 처리
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
curl_close($ch);
return false;
}
curl_close($ch);
// Simple HTML DOM Parser 로딩 - 이부분은 첨부파일 다운로드 후 각자 설정에 맞게 수정하세요.
include_once($_SERVER['DOCUMENT_ROOT']."/app/simplehtmldom/simple_html_dom.php");
// HTML 파싱
$dom = new simple_html_dom();
$dom->load($html);
// 파비콘 추출
$favicon = '';
$faviconElement = $dom->find('link[rel="icon"], link[rel="shortcut icon"]', 0);
if ($faviconElement) {
$favicon = $faviconElement->href;
// "http://" 또는 "https://"가 생략된 경우 처리
if (!parse_url($favicon, PHP_URL_SCHEME)) {
$favicon = rtrim($url, '/') . '/' . ltrim($favicon, '/');
}
} else {
// 파비콘이 없는 경우 메타 태그에서 이미지 추출
$imageElement = $dom->find('meta[property="og:image"], meta[name="twitter:image"], meta[itemprop="image"]', 0);
if ($imageElement) {
$favicon = $imageElement->content;
// "http://" 또는 "https://"가 생략된 경우 처리
if (!parse_url($favicon, PHP_URL_SCHEME)) {
$favicon = rtrim($url, '/') . '/' . ltrim($favicon, '/');
}
}
}
// 제목 추출
$title = '';
$titleElement = $dom->find('title', 0);
if ($titleElement) {
$title = $titleElement->plaintext;
}
// 설명 추출
$description = '';
$descriptionElement = $dom->find('meta[property="og:description"], meta[name="twitter:description"]', 0);
if ($descriptionElement) {
$description = $descriptionElement->content;
}
// 메모리 해제
$dom->clear();
unset($dom);
return [
'url' => $url,
'favicon' => $favicon,
'title' => $title,
'description' => $description
];
}
[/code]
플러터로 즐겨찾기 앱(웹)을 만들려고 준비한 건데
나름 잘 되는 거 같아서 올려봅니다 :)
게시글 목록
| 번호 | 제목 |
|---|---|
| 16429 |
기타
html 색상표
1
|
| 16424 | |
| 16423 |
node.js
NODE_JS + Nunjucks 설정
|
| 16412 |
PHP
한글 초성 추출하기
10
|
| 16408 | |
| 16407 |
JavaScript
input text 에 여러 이벤트 걸기 두기.
1
|
| 16401 | |
| 16395 |
JavaScript
Javascript를 최적화하는 13가지 팁
4
|
| 16394 |
JavaScript
URL이 올바른지 체크
2
|
| 16391 |
JavaScript
IE, Edge 브라우저에서는 해당 사이트가 접속이 안되게 하는 코드
2
|
| 16390 |
JavaScript
구글 맵 독도 표기 되도록.
|
| 16389 | |
| 16387 | |
| 16386 |
JavaScript
js playground
|
| 16382 |
JavaScript
javascript에서 php를 호출하여 내용 변경하기
|
| 16381 | |
| 16377 | |
| 16374 | |
| 16372 | |
| 16356 |
기타
기시판 질문입니다.
6
|
| 16355 |
정규표현식
정규식 테스트 연습 편하게
2
|
| 16354 |
정규표현식
제목에 특수문자 정리하는 정규식
2
|
| 16353 | |
| 16347 | |
| 16346 | |
| 16339 | |
| 16338 |
jQuery
체크박스 시프트(shift) 멀티선택
|
| 16332 |
node.js
Node.js MongoDB Drop
1
|
| 16331 |
node.js
여러 데이터 삭제
1
|
| 16330 |
node.js
Node.js MongoDB 삭제
|
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기