사이트 정보(파비콘, 제목, 설명) 가져오는 함수
[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]
플러터로 즐겨찾기 앱(웹)을 만들려고 준비한 건데
나름 잘 되는 거 같아서 올려봅니다 :)
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4916 | 웹서버 | 6년 전 | 2861 | ||
| 4915 | PHP |
|
6년 전 | 2174 | |
| 4914 | 웹서버 | 6년 전 | 1888 | ||
| 4913 | JavaScript | 6년 전 | 2595 | ||
| 4912 | node.js | 6년 전 | 3706 | ||
| 4911 | 기타 | 6년 전 | 4188 | ||
| 4910 | 기타 | 6년 전 | 2272 | ||
| 4909 | 기타 | 6년 전 | 1977 | ||
| 4908 | 기타 | 6년 전 | 2009 | ||
| 4907 | Mobile | 7년 전 | 2280 | ||
| 4906 | JavaScript | 7년 전 | 2281 | ||
| 4905 | 기타 | 7년 전 | 2270 | ||
| 4904 | jQuery | 7년 전 | 2511 | ||
| 4903 | PHP | 7년 전 | 5223 | ||
| 4902 | jQuery | 7년 전 | 5127 | ||
| 4901 | 기타 | 7년 전 | 2703 | ||
| 4900 | MySQL | 7년 전 | 4110 | ||
| 4899 | 기타 | 7년 전 | 2264 | ||
| 4898 | 웹서버 | 7년 전 | 2446 | ||
| 4897 | MySQL | 7년 전 | 2197 | ||
| 4896 | MySQL | 7년 전 | 2632 | ||
| 4895 | JavaScript | 7년 전 | 9767 | ||
| 4894 | 웹서버 | 7년 전 | 2354 | ||
| 4893 | 기타 | 7년 전 | 8313 | ||
| 4892 | jQuery | 7년 전 | 5677 | ||
| 4891 | 기타 | 7년 전 | 2830 | ||
| 4890 | PHP | 7년 전 | 3413 | ||
| 4889 | JavaScript | 7년 전 | 6374 | ||
| 4888 | MySQL | 7년 전 | 3132 | ||
| 4887 | MySQL | 7년 전 | 2761 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기