using regex to separate local, youtube or vimeo video - php

I am making a video playback website and I need to use a html5 video player to play local videos and youtube api and vimeo api to play vimeo videos. THE problem is I want to filter the link entered by the user for all 3 conditions. I tried some regex as I never used it before but was not successful. Can anyone help me giving a example showing how to separate those links for making 3 conditions
taking out the word "youtube" out of a youtube link so that i could make a condition statement
same way taking out .mp4 and .ogg extension from the local video so that it can be used to be played in a html5 video player
same way extract the word "vimeo" from a vimeo link so that I can use a vimeo api to play vimeo videos.
Can anyone help?
For example we input a youtube link
http://www.youtube.com/watch?v=example
Then how to get the word "youtube" out of this link?
And I uploaded a local video with file name
example.mp4
Then how can I get the extensoon mp4 from that?

This is adapated from a php class I wrote some times ago :
<?php
function identifyService ( $url )
{
$url = preg_replace('#\#.*$#', '', trim($url));
$services_regexp = array(
"#^\w+\.(?P<format>[a-zA-Z0-9]{2,5})#" => 'local',
'#vimeo\.com\/(?P<id>[0-9]*)[\/\?]?#i' => 'vimeo',
'#youtube\.[a-z]{0,5}/.*[\?&]?v(?:\/|=)?(?P<id>[^&]*)#i' => 'youtube'
);
foreach ( $services_regexp as $pattern => $service ) {
if ( preg_match ( $pattern, $url, $matches ) ) {
return ( $service === 'local' ) ? $matches['format'] : $service;
}
}
return false;
}
$url = "foo.ogg";
// $url = "bar.mp4";
// $url = "http://www.youtube.com/watch?v=_rqR8_mOp_A";
// $url = "http://vimeo.com/39044814";
$service = identifyService( $url );
var_dump( $service );
?>
Hope it helps.

Related

Uploaded video via DailyMotion API is not watchable by other users

I am using PHP SDK to upload videos to my DailyMotion channel, but i don't understand how this work.
I do this:
// Temporarily upload a file on Dailymotion' servers
// This does not create a video, it only offers you a public URL to work with.
$url = $api->uploadFile($videoPath);
try {
// More fields may be mandatory in order to create a video.
// Please refer to the complete API reference for a list of all the required data.
$result = $api->post(
'/me/videos',
array(
'url' => $url,
'title' => $title,
'tags' => 'video,publish,api',
'published' => true,
'is_created_for_kids' => false
)
);
print_r($result);
} catch(\Exception $e) {
die('exception: ' . $e->getMessage());
}
$api->logout();
When the video is uploaded, i keep getting a message saying "Publication in progress" for days (a week now and still getting the same message).
Although i can see/watch the video just fine if i used the account it belongs to.
I'm using 'GRANT_TYPE_PASSWORD' to upload the videos to my account.
I don't want my videos to be published in search results though, i just want my app users to be able to watch them embeded.
Any help would be highly appreciated, thanks in Advance.

Vimeo API upload set title and description

I am trying to upload vimeo files to a vimeo account via their PHP API.
This is my code ( we upload first to server in $[FILES][tmp_name] then we upload that file to vimeo with their php api ) -
$path = $_FILES['myfile']['tmp_name'];
$feedback = $vim->upload($path);
It works fine, just that I want it to be able to be uploaded with a title and a description.
Right now they are uploaded with the Untitled name.
There is no mention of title in their php api - https://github.com/vimeo/vimeo.php/blob/master/src/Vimeo/Vimeo.php
nor their api - https://developer.vimeo.com/api/upload/videos
Any ideas ?
Got it!
It's
$path = $_FILES['myfile']['tmp_name'];
$feedback = $vim->upload($path);
$vim->request($feedback, array(
'name' => 'ceva'
,'description' => 'alceva'
), 'PATCH');

How do I can post a multiple photos via Facebook API

Now I posting a single photo to wall like this:
$response = $facebook->api("/$group_id/photos", "POST", array(
'access_token=' => $access_token,
'message' => 'This is a test message',
'url' => 'http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg',
)
);
It works fine, but can I somehow post a multiple photos, something like this:
You can now publish multiple images in a single post to your feed or page:
For each photo in the story, upload it unpublished using the {user-id}/photos endpoint with the argument published=false.
You'll get an ID for each photo you upload like this:
{
"id": "10153677042736789"
}
Publish a multi-photo story using the {user-id}/feed endpoint and using the ids returned by uploading a photo
$response = $facebook->api("/me/feed", 'POST',
array(
'access_token=' => $access_token,
'message' => 'Testing multi-photo post!',
'attached_media[0]' => '{"media_fbid":"1002088839996"}',
'attached_media[1]' => '{"media_fbid":"1002088840149"}'
)
);
Source: Publishing a multi-photo story
You can make batch requests as mentioned here: https://stackoverflow.com/a/11025457/1343690
But its simple to loop through your images and publish them directly.
foreach($photos as $photo)
{
//publish photo
}
Edit: (regarding grouping of photos on wall)
This grouping is done by facebook automatically if some photos are uploaded into the same album.
Currently you cannot create an album in a group via Graph API - it is not supported (as of now), see this bug.
But you can do this - create an album manually, then get the album_id by-
\GET /{group-id}/albums, then use the the code with album_id instead of group_id-
foreach($photos as $photo){
$facebook->api("/{album-id}/photos", "POST", array(
'access_token=' => $access_token,
'name' => 'This is a test message',
'url' => $photo
)
);
}
I've tested it, see the result-
Actually you can upload a multi story photo(I did it using Graph Api and PHP) but the problem comes if you need scheduled this post.Your post is schedule but also it shows on the page's feed.
P.S. I'm using Graph Api v2.9
PHP Code
$endpoint = "/".$page_id."/photos";
foreach ($multiple_photos as $file_url):
array_push($photos, $fb->request('POST',$endpoint,['url' =>$file_url,'published' => FALSE,]));
endforeach;
$uploaded_photos = $fb->sendBatchRequest($photos, $page_access_token);
foreach ($uploaded_photos as $photo):
array_push($data_post['attached_media'], '{"media_fbid":"'.$photo->getDecodedBody()['id'].'"}');
endforeach;
$data_post['message'] = $linkData['caption'];
$data_post['published'] = FALSE;
$data_post['scheduled_publish_time'] = $scheduled_publish_time;
$response = $fb->sendRequest('POST', "/".$page_id."/feed", $data_post, $page_access_token);
$post_id = $cresponse->getGraphNode()['id'];
You will need to upload each photo first with published state to false, and then use the ID's of the unpublished photos to the /me/feed endpoint to schedule the photo. The schedule needs to be within the 24 hours from the time the photos are uploaded as facebook deletes all unpublished photos in 24 hours.
Ref:
https://developers.facebook.com/docs/graph-api/photo-uploads/
There is no way to publish more than one photo in the same graph API call.
See documentation: https://developers.facebook.com/docs/graph-api/reference/user/photos

Determining the Vimeo source by URL (regex)

Okay, here's a fun one.
I need to figure out what source a user is referencing when entering a copy and pasted Vimeo URL into an input box. For those that are unfamiliar, Vimeo currently has 4 different sources that are accessible through their simple API:
Videos belonging to a user
Valid URL structure: http://vimeo.com/user3825208 or https://vimeo.com/davekiss
Videos belonging to a group
Valid URL structure: http://vimeo.com/groups/eos550d or https://vimeo.com/groups/162
Videos belonging to a channel
Valid URL structure: http://vimeo.com/channels/hd or https://vimeo.com/channels/201
Videos belonging to an album
Valid URL structure: http://vimeo.com/album/1919683 or https://vimeo.com/album/mycustomname
So basically, I want to be able to run the URL into a function which will tell me what source the URL belongs to.
I've been using this for videos belonging to a user, but now I need to expand to all sources.
sscanf(parse_url($url, PHP_URL_PATH), '/%d', $video_id);
Maybe I should do this four times? preg_match('???', $url);
Thanks for your help!
You don't need regular expressions:
function discoverVimeo($url)
{
if ((($url = parse_url($url)) !== false) && (preg_match('~vimeo[.]com$~', $url['host']) > 0))
{
$url = array_filter(explode('/', $url['path']), 'strlen');
if (in_array($url[0], array('album', 'channels', 'groups')) !== true)
{
array_unshift($url, 'users');
}
return array('type' => rtrim(array_shift($url), 's'), 'id' => array_shift($url));
}
return false;
}
The following will return an array, with an index id and another index type, that will be one of:
user
album
channel
group
i would preg_match() the following regex patterns (in this order):
$channel_regex = '%vimeo\.com/channels/([a-zA-Z0-9]+)%/i';
$group_regex = '%vimeo\.com/groups/([a-zA-Z0-9]+)%/i';
$album_regex = '%vimeo\.com/album/([a-zA-Z0-9]+)%/i';
$user_regex = '%vimeo\.com/([a-zA-Z0-9]+)%/i';
this will regex match for:
vimeo.com/channels/...grab_this_data...
vimeo.com/groups/...grab_this_data...
vimeo.com/albums/...grab_this_data...
and if all of those preg_matches fail, (therefore a user URL), it will grab whatever is in the url:
vimeo.com/...grab_this_data...
good luck.

tagging a page in photos

Greetings
Here's what's up:
I'm working on an app where the user or a page they administrate is tagged in their own image, however, tagging a page they administrate doesn't function, I have no trouble with the user being tagged.
Here's some code:
$tdata = array('tag_uid' => $fb_id,'x' => 0,'y' => 0);
$datatags[] = $tdata;
$attachment = array(
'access_token' => $access_token,
'tags' => $datatags
);
$attachment['image'] = '#'.realpath($image_name.);
$result = $facebook->api('/'.$album_id.'/photos', 'POST', $attachment);
$fb_id is either the ID for the user or the page. which is grabbed using /me/accounts in the Graph API
Thanks!
Per the documentation you cannot do this:
You can specify which user to tag using two methods: in the URL path
as PHOTO_ID/tags/USER_ID, or in a URL parameter as
PHOTO_ID/tags?to=USER_ID. Currently, you cannot tag a Page in a photo
using this API.
https://developers.facebook.com/docs/reference/api/photo/

Categories