사이트 정보(파비콘, 제목, 설명) 가져오는 함수

· 2023-05-19 (금) 10:09:31 · 1823

[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]

 

플러터로 즐겨찾기 앱(웹)을 만들려고 준비한 건데

나름 잘 되는 거 같아서 올려봅니다 :)

 
 

 

 

첨부파일

simple_html_dom.php (57.9 KB) 8회 2023-05-19 10:13
|
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
기타 23-07-03 조회 1,569
MySQL 23-07-02 조회 2,238
JavaScript 23-07-01 조회 1,872
PHP 23-06-30 조회 2,870
MySQL 23-06-29 조회 7,639
node.js 23-06-28 조회 1,903
node.js 23-06-27 조회 1,474
PHP 23-06-26 조회 1,704
PHP 23-06-25 조회 1,731
PHP 23-06-24 조회 1,209
PHP 23-06-22 조회 1,500
PHP 23-06-20 조회 1,574
기타 23-06-19 조회 1,841
23-06-17 조회 1,246
기타 23-06-18 조회 2,303
PHP 23-06-17 조회 1,600
JavaScript 23-06-16 조회 2,295
PHP 23-06-15 조회 1,957
23-05-16 조회 1,178
기타 23-06-13 조회 1,611
PHP 23-06-05 조회 1,696
PHP 23-05-28 조회 2,013
기타 23-05-23 조회 2,353
PHP 23-05-19 조회 1,824
PHP 23-05-18 조회 1,965
PHP 23-05-08 조회 1,924
JavaScript 23-04-10 조회 1,706
MySQL 23-04-08 조회 1,929
MySQL 23-03-15 조회 1,944
PHP 23-03-14 조회 2,318