How to get live video id from from youtube channel html - php

How to get live video id from YouTube channel using simple HTML dom parser or any other method rather than YouTube api?
https://www.youtube.com/embed/live_stream?channel=UC8Z-VjXBtDJTvq6aqkIskPg&autoplay=1
Because YouTube api does not work to get live video id.

Finaly i fund answer
function getvideourl($chid)
{
$videoId = null;
// Fetch the livestream page
if($data = file_get_contents('https://www.youtube.com/embed/live_stream?
channel='.$chid))
{
// Find the video ID in there
if(preg_match('/\'VIDEO_ID\': \"(.*?)\"/', $data, $matches))
$videoId = $matches[1];
else
$videoId ="";
}
else
throw new Exception('Couldn\'t fetch data');
$video_url = "https://www.youtube.com/embed/".$videoId;
return $video_url;
}

Related

Allow users to upload videos to my channel using youtube API v3

Using Google API v3 php library . I want the user to upload videos on my youtube channel. But oAuth require user google login and the video uploaded to the logged in user youtube channel.
Before using the V3 api we used the V2 to upload the video and it works well.
global $youtube_api_key, $youtube_username, $youtube_password;
if(is_file('../uploader/ClassYouTubeAPI.php')){ include_once ('../uploader/ClassYouTubeAPI.php'); }
else{ include_once('ClassYouTubeAPI.php'); }
$obj = new ClassYouTubeAPI($youtube_api_key);
$result = $obj->clientLoginAuth($youtube_username, $youtube_password);
$result = $obj->uploadVideo($uploaded_file_name, $file_path, $title, $description, $privacy);
var_dump($result);
if (is_array($result) and count($result) and ! isset($result["is_error"])) {
$youtube_file = str_replace($uploaded_file_name, $result["videoId"] . '.youtube', $file_path);
$resource = fopen($youtube_file, 'w');
fwrite($resource, "");
fclose($resource);
#unlink($file_path);
return $result["videoId"];
} else {
#unlink($file_path);
return false;
}
Is there any way to use the V3 without 'User Google Account' and upload the video to my channel?
There is only one solution, described here.
In short, you must create a "web application" account (not a "service account") in Google console and do authentication from your server on behalf of your YouTube account.

How to post on fb using php?

I am using facebook apis to post on my testing account using php. The following code does the job for me and it works. If I log in with my account I can see the post, but if I use another account the post does not show. I have modified the privacy setting to public but still. I went to facebook's documentation,but I couldn't really find something to solve my problem. Can anyone help???
Thanks in advanced
function fb_post($post, $msg, $url, $pic) {
$temp_array = queryFBTable($post);
if (count($temp_array) == 0) return;
// initialize Facebook class using your own Facebook App credentials
// see: https://developers.facebook.com/docs/php/gettingstarted/#install
$config = array();
$config['appId'] = 'xxxxxxxxxxx';
$config['secret'] = 'xxxxxxxxxxxxxxxx';
$config['fileUpload'] = true; // optional
$fb = new Facebook($config);
// define your POST parameters (replace with your own values)
// see: https://developers.facebook.com/docs/facebook-login/access-tokens/
$params = array();
if(isset($temp_array['AccessToken'])) {
$params['access_token'] = $temp_array['AccessToken'];
}
if(isset($msg)) {
$params['message'] = $msg;
}
if(isset($url)) {
$params['link'] = $url;
}
if(isset($pic)) {
$params['picture'] = $pic;
}
//"name" => "yooo",
//"caption" => "www.pontikis.net",
//"description" => "Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation."
// post to Facebook
// see: https://developers.facebook.com/docs/reference/php/facebook-api/
try {
//tim .. 1471145939793099
// van .. 1491737127730006
// mc .. 796523467054680
$ret = $fb->api("/".$temp_array['userID']."/feed", 'POST', $params);
echo "<br>Successfully posted to Facebook account ".$post.". <br/>";
}
catch(Exception $e) {
echo $e->getMessage();
}
}

Fetch YouTube Video Id from youtube URL in zend framework

This is my youtube tube URL which is coming from facebook Id. I have fetch this url using FQL Query in facebook in Zend Framework
This is my url = "http://www.youtube.com/attribution_link?a=AbE6fYtNaa4&u=%2Fwatch%3Fv%3DNbyHNASFi6U%26feature%3Dshare"
Now I need to fetch its ID so that I can pass it to this code so that i can generate its vidoe details.
function listYoutubeVideo($id) {
$video = array();
try {
$yt = new Zend_Gdata_YouTube();
$videoEntry = $yt->getVideoEntry($id);
$videoEntry = $yt->getQueryUrl($id);
$videoThumbnails = $videoEntry->getVideoThumbnails();
$video = array(
'thumbnail' => $videoThumbnails[0]['url'],
'title' => $videoEntry->getVideoTitle(),
'description' => $videoEntry->getVideoDescription(),
'tags' => implode(', ', $videoEntry->getVideoTags()),
'url' => $videoEntry->getVideoWatchPageUrl(),
'flash' => $videoEntry->getFlashPlayerUrl(),
'dura' => $videoEntry->getVideoDuration(),
'id' => $videoEntry->getVideoId()
);
} catch (Exception $e) {
echo $e->getMessage();
exit();
}
return $video;
}
So I just need to find its youtube ID from youtube URL in Zend Framework. Plz provide me solutions. Is there any method exist in "Zend_Gdata_YouTube" class from where i can get its ID form passing its youtube URL
You have unique video ID. In your case (https://www.youtube.com/watch?v=NbyHNASFi6U&feature=share) ID equals NbyHNASFi6U.
$url = 'http://www.youtube.com/attribution_link?a=AbE6fYtNaa4&u=%2Fwatch%3Fv%3DNbyHNASFi6U%26feature%3Dshare';
$url = urldecode($url);
$query_string = end(explode('?',$url));
parse_str($query_string);
$videoID = $v;
print $videoID;
// OUTPUT
NbyHNASFi6U
The video watch page URL always has this format:
http://www.youtube.com/watch?v=XXXXXXXXXXX
where XXXXXXXXXXX is a unique video ID consisting of 11 letters and numbers (and, if memory serves, hyphens and underscores).
public function get_id($url)
{
$ytshorturl = 'youtu.be/';
$ytlongurl = 'www.youtube.com/watch?v=';
if (strpos($url, $ytshorturl) !== false) {
$url = str_replace($ytshorturl, $ytlongurl, $url);
}
$components = parse_url($url);
parse_str($components['query'], $results);
return $results['v'];
}
$url = "https://youtu.be/oeg192rQ1xE"; //sample url
print(get_id($url));

Get a video link from youtube

In one of my application I need to show a YouTube video. If user submits a video then I have to check if that video is alive in YouTube. If OK then I have to save the video id in my database and generate video in web page.
Is there any method for validating YouTube video ?
Use this class to extract and validate youtube video. This works for YT urls like /embed/ , /v/ , ?v= /, youtu.be
class Youtube {
///// Put together by Sugato
////////// $video_id is the youtube video ID /////////////////////////
public $video_id = null;
///////// the Constructer ////////////////////////////////////////
public function __construct($url)
{
if (preg_match('/youtube\.com\/watch\?v=([^\&\?\/]+)/', $url, $id)) {
$this->video_id = $id[1];
} else if (preg_match('/youtube\.com\/embed\/([^\&\?\/]+)/', $url, $id)) {
$this->video_id = $id[1];
} else if (preg_match('/youtube\.com\/v\/([^\&\?\/]+)/', $url, $id)) {
$this->video_id = $id[1];
} else if (preg_match('/youtu\.be\/([^\&\?\/]+)/', $url, $id)) {
$this->video_id = $id[1];
} else {
$this->video_id = NULL;
}
}
/////////// validates if a youtube video actually exists //////////////
function validate()
{
if(empty($this->video_id))
{
return false;
}
else {
$curl = curl_init("http://gdata.youtube.com/feeds/api/videos/" . $this->video_id);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec($curl);
$request = curl_getinfo($curl);
curl_close($curl);
$result = explode(";", $request["content_type"]);
if($result[0] == "application/atom+xml")
{
return true;
} else {
return false;
}
}
}
}
Call the class like this
$yt = new Youtube($your_video_link_here);
$exist = $yt->validate();
if($exist)
{
echo "Yaaaayyyyyy!";
} else
{
echo "nAAAAyyyy!!!";
}
If the user is flat-out submitting a video, you would have to have something like a database which contains hashes for existing videos to compare it with (ex: the SHA checksum), then check if the hash is already present. As far as I know, Google/YouTube provide no such database for the public to use, but you could start your own for the videos that users submit through your service. There are other more advanced techniques you could use, but they would require access to all of the existing video files for analysis... which is not available.
As far as getting the video URL, when you upload a video to YouTube you can link to it or embed it in a webpage.

Get the Video URL after uploading a video to Youtube using Data API - php

I'm trying to upload a video to YouTube using the PHP Data API
$yt = new Zend_Gdata_YouTube($httpClient);
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$filesource = $yt->newMediaFileSource('mytestmovie.mov');
$filesource->setContentType('video/quicktime');
$filesource->setSlug('mytestmovie.mov');
$myVideoEntry->setMediaSource($filesource);
$myVideoEntry->setVideoTitle('My Test Movie');
$myVideoEntry->setVideoDescription('My Test Movie');
// Note that category must be a valid YouTube category !
$myVideoEntry->setVideoCategory('Comedy');
// Set keywords, note that this must be a comma separated string
// and that each keyword cannot contain whitespace
$myVideoEntry->SetVideoTags('cars, funny');
// Optionally set some developer tags
$myVideoEntry->setVideoDeveloperTags(array('mydevelopertag',
'anotherdevelopertag'));
// Optionally set the video's location
$yt->registerPackage('Zend_Gdata_Geo');
$yt->registerPackage('Zend_Gdata_Geo_Extension');
$where = $yt->newGeoRssWhere();
$position = $yt->newGmlPos('37.0 -122.0');
$where->point = $yt->newGmlPoint($position);
$myVideoEntry->setWhere($where);
// Upload URI for the currently authenticated user
$uploadUrl =
'http://uploads.gdata.youtube.com/feeds/users/default/uploads';
// Try to upload the video, catching a Zend_Gdata_App_HttpException
// if availableor just a regular Zend_Gdata_App_Exception
try {
$newEntry = $yt->insertEntry($myVideoEntry,
$uploadUrl,
'Zend_Gdata_YouTube_VideoEntry');
} catch (Zend_Gdata_App_HttpException $httpException) {
echo $httpException->getRawResponseBody();
} catch (Zend_Gdata_App_Exception $e) {
echo $e->getMessage();
}
Does anyone know how to get the URL of the uploaded video from the $newEntry object.
Any help will be appreciated :)
Try this:
try {
$newEntry = $yt->insertEntry($myVideoEntry, $uploadUrl,
'Zend_Gdata_YouTube_VideoEntry');
$id = $newEntry->getVideoId(); // YOUR ANSWER IS HERE :)
echo $id;
}

Categories