채택완료

youtube api로 동영상 제목 가져오는법

유튜브 api key를 발급 받앗는데 

원하는 동영상에서 제목을 추출해 낼려면 어떡해야할까요..?

|

답변 1개 / 댓글 5개

채택된 답변
+20 포인트
Copy
/**
 * @param $videoId
 * @return object{
 * publishedAt, channelId,
 * title, description, thumbnails, channelTitle, tags, categoryId,
 * liveBroadcastContent, defaultLanguage, localized{title, description}, defaultAudioLanguage
 * }
 * https://developers.google.com/youtube/v3/docs/videos?hl=ko
 */
function getYoutubeInfo($videoId) {
  $apikey = 'your_APIkey';
  $content = curl('https://www.googleapis.com/youtube/v3/videos?id=' . $videoId . '&key=' . $apikey . '&part=snippet,contentDetails,statistics,status');

  $content = json_decode($content, true);
  // 영상이 없거나 비공개인경우
  if ($content[pageInfo][totalResults] == 0) {
    return null;
  }

  return $content[items][0];
}
 

우선 유툽 인포메이션 가져오고

Copy
$info = getYoutubeInfo($row[videoId]);


$snippet = $info[snippet];
$contentDetails = $info[contentDetails];

 


$snippet[title]; //제목



$snippet[description]; // 내용


$contentDetails[duration]; // 영상시간

정도로 가져올수있고 

https://developers.google.com/youtube/v3/docs/videos?hl=ko  여기 보시면 더 정확하게 나와있어요

답변에 대한 댓글 5개

[code]

/**
* @param $url
* @return bool|string
*/
function curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$g = curl_exec($ch);
curl_close($ch);
return $g;
}
[/code]



빠진 부분 추가해요
자바스크립트로 쓸 수 잇을까요?
혹시 id 가져오는부분도 찾으실까봐 url 입력하면 id 찾아주는 함수에요
[code]
/**
* @param $contents
* @return false|mixed
* 유튜브 ID 가져오기
*/
function get_youtube_id($contents) {
if(!$contents)
return false;

preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $contents, $match);

return $match[1];
}
[/code]
자바스크립트는 소스가 더 많이 있을거에요 구현 해둔건 없어서 비슷하게 하시면 될겁니다.
너무 감사합니다 ㅠ

답변을 작성하려면 로그인이 필요합니다.