Wednesday, 15 January 2014

forms - PHP file insted of showing output on screen create a csv file if it does not exist then add data to it -



forms - PHP file insted of showing output on screen create a csv file if it does not exist then add data to it -

okay have next php file output youtube video title description , thumb url on screen using echo statements.

is there way can every time user submits video id instead of outputting text on screen echo message done

then want check if id.csv exists if not create id.csv else carry on

then want , title description , thumb url in separate cells side side

i not have much knowledge on php , csv help examples help

find php script below

video id:

<?php //the youtube's api url define('yt_api_url', 'http://gdata.youtube.com/feeds/api/videos?q='); //change below video id. $video_id = $_post['vid']; //using curl php extension create request youtube api $ch = curl_init(); curl_setopt($ch, curlopt_url, yt_api_url . $video_id); curl_setopt($ch, curlopt_returntransfer, 1); //$feed holds rss feed xml returned youtube api $feed = curl_exec($ch); curl_close($ch); //using simplexml parse youtube's feed $xml = simplexml_load_string($feed); $entry = $xml->entry[0]; //if no entry whas found, youtube didn't find video specified id if(!$entry) exit('error: no video id "' . $video_id . '" whas found. please specify id of existing video.'); $media = $entry->children('media', true); $group = $media->group; $title = $group->title;//$title: video title $desc = $group->description;//$desc: video description $vid_keywords = $group->keywords;//$vid_keywords: video keywords $thumb = $group->thumbnail[0];//there 4 thumbnails, first 1 (index 0) largest. //$thumb_url: url of thumbnail. $thumb_width: thumbnail width in pixels. //$thumb_height: thumbnail height in pixels. $thumb_time: thumbnail time in video list($thumb_url, $thumb_width, $thumb_height, $thumb_time) = $thumb->attributes(); $content_attributes = $group->content->attributes(); //$vid_duration: duration of video in seconds. ex.: 192. $vid_duration = $content_attributes['duration']; //$duration_formatted: duration of video formatted in "mm:ss". ex.:01:54 $duration_formatted = str_pad(floor($vid_duration/60), 2, '0', str_pad_left) . ':' . str_pad($vid_duration%60, 2, '0', str_pad_left); //echoing variables testing purposes: echo 'title: ' . $title . '<br />'; echo 'desc: ' . $desc . '<br />'; echo 'thumbnail url: ' . $thumb_url . '<br />'; ?>

php forms csv http-post

No comments:

Post a Comment