Friday, 12 July 2013

Search lists from Youtube, Dailymotion, Google and Metacafe using PHP

Youtube:  For youtube you need to down the files from google-api-php-client and please use this below demo file


$htmlBody = <<

 

    Search Term:
 
 

    Max Results:
 
 

END;

if ($_GET['q'] && $_GET['maxResults']) {
  // Call set_include_path() as needed to point to your client library.
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_YouTubeService.php';

  /* Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the
  Google APIs Console
  Please ensure that you have enabled the YouTube Data API for your project. */
  $DEVELOPER_KEY = 'put your api key here';

  $client = new Google_Client();
  $client->setDeveloperKey($DEVELOPER_KEY);

  $youtube = new Google_YoutubeService($client);

  try {
    $searchResponse = $youtube->search->listSearch('id,snippet', array(
      'q' => $_GET['q'],
      'maxResults' => $_GET['maxResults'],
    ));

    $videos = '';
    $channels = '';
    $playlists = '';


//print_r($searchResponse['items']);



for($x=0;$x
echo $searchResponse['items'][$x]['id'] ['videoId'] ;


}

/*
    foreach ($searchResponse['items'] as $searchResult) {
      switch ($searchResult['id']['kind']) {
        case 'youtube#video':
          $videos .= sprintf('

  • %s (%s)
  • ', $searchResult['snippet']['title'],
                $searchResult['id']['videoId']);
              break;
            case 'youtube#channel':
              $channels .= sprintf('
  • %s (%s)
  • ', $searchResult['snippet']['title'],
                $searchResult['id']['channelId']);
              break;
            case 'youtube#playlist':
              $playlists .= sprintf('
  • %s (%s)
  • ', $searchResult['snippet']['title'],
                $searchResult['id']['playlistId']);
              break;
          }
        }

        $htmlBody .= <<   

    Videos

       
      $videos


       

    Channels


       
      $channels

       

    Playlists


       
      $playlists

    END;*/
      } catch (Google_ServiceException $e) {
        $htmlBody .= sprintf('A service error occurred: %s
    ',
          htmlspecialchars($e->getMessage()));
      } catch (Google_Exception $e) {
        $htmlBody .= sprintf('An client error occurred: %s
    ',
          htmlspecialchars($e->getMessage()));
      }
    }
    ?>



     
        YouTube Search
     

     
       
     



    =================================================================

    Google Image Search:



    $url = "https://ajax.googleapis.com/ajax/services/search/images?" .
           "v=1.0&q=barack%20obama&userip=INSERT-USER-IP";

    // sendRequest
    // note how referer is set manually
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, "http://localhost/#######/google.php");
    $body = curl_exec($ch);
    curl_close($ch);

    // now, process the JSON string
    $json = json_decode($body);

    /*$results = array();
    foreach ($json['responseData']['results'] as $result) {
      $results[] = array(
        'url' => $result['url'],
        'title' => $result['title'],
        'content'=>$result['content']
      );
    }
    */
    echo '
    ';
    
    var_dump($json->responseData->results);
    echo '
    ';


    for($x=0;$xresponseData->results);$x++){

    echo "Result ".($x+1)."";
    echo "
    Image: ";
    echo $json->responseData->results[$x]->url;
    echo "


    echo $json->responseData->results[$x]->url;
    echo "
    URL: ";
    echo $json->responseData->results[$x]->url;
    echo "
    VisibleURL: ";
    echo $json->responseData->results[$x]->visibleUrl;
    echo "
    Title: ";
    echo $json->responseData->results[$x]->title;
    echo "
    Content: ";
    echo $json->responseData->results[$x]->content;
    echo "

    ";

    }

    // now have some fun with the results...

    /*echo '

    ';
    var_dump($json);
    echo '
    ';
    */


    //$decodedJSON = json_encode($json);

    // Put everyting to the screen with var_dump;
    //var_dump($decodedJSON);

    // With print_r ( useful for arrays );
    //print_r($decodedJSON);

    /*foreach ( $json->responseData->results as $results1 )
    {
           echo $results1;
    }*/

    //print_r($json->responseData->results);

    /*$properties = get_object_vars($json);
    print_r($properties);*/

    //print_r($json);

    ?>


    ==================================================================

    Metacafe:



    if(isset($_POST['search']))
    {
        $video=$_POST['video'];
        //echo $video;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'http://www.metacafe.com/topics/'.$video);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
        $result = curl_exec($curl); curl_close($curl);
        //echo $result;
       
        //$value=preg_match_all('/<section class=\"CCol">(.*?)<\/section>/s',$result,PREG_PATTERN_ORDER);
        preg_match('/
    (.*?)<\/section>/s', $result, $matches, PREG_OFFSET_CAPTURE);
        print_r($matches);
    }
    ?>
    http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd

    ">
    http://www.w3.org/1999/xhtml">


    Search Video - Metcafe




    ==================================================================

    Daily Motion:


    $url="https://api.dailymotion.com/videos?search=facebook&fields=id,title,channel,thumbnail_medium_url";
    // sendRequest
    // note how referer is set manually
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, "http://localhost/jothirajan/demos/dailymotion-sdk-php-master/test.php");

    $body = curl_exec($ch);
    curl_close($ch);

    // now, process the JSON string
    $json = json_decode($body);

    /*echo '
    ';
    var_dump($json);
    echo '
    ';

    */
    for($x=0;$xlist);$x++){


    echo "Result ".($x+1)."";
    echo "
    id: ";
    echo $json->list[$x]->id;
    echo "
    Img: ";
    echo "
    ";

    echo "
    channel: ";
    echo $json->list[$x]->channel;
    echo "
    title: ";
    echo $json->list[$x]->title;

    echo "

    ";

    }

    ?>





    Thursday, 2 May 2013

    Increment date using while loop in php



     $start_date = "2013-01-01";
      $end_date = "2013-01-20";
      $next_date = $start_date;

      while(strtotime($next_date) <= strtotime($end_date))
      {
        echo "$next_date
    ";
        $next_date = date ("Y-m-d", strtotime("+5 day", strtotime($next_date)));
      }


    Ok in for loop?


    $start_date = "2013-01-01";
    $end_date = "2013-01-25";
    $next_date = $start_date;

    for($i=0;$i<=(strtotime($next_date) <= strtotime($end_date));$i++)
    {
    echo "$next_date
    ";
    $next_date = date ("Y-m-d", strtotime("+2 day", strtotime($next_date)));
    }





    SUM TWO TIMES IN PHP, SUM TWO TIME VALUES IN PHP

    echo sum_the_time('01:45:22', '17:27:03');

    function sum_the_time($time1, $time2) {
      $times = array($time1, $time2);
      $seconds = 0;
      foreach ($times as $time)
      {
        list($hour,$minute,$second) = explode(':', $time);
        $seconds += $hour*3600;
        $seconds += $minute*60;
        $seconds += $second;
      }
      $hours = floor($seconds/3600);
      $seconds -= $hours*3600;
      $minutes  = floor($seconds/60);
      $seconds -= $minutes*60;
      // return "{$hours}:{$minutes}:{$seconds}";
      return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);


    Thursday, 14 March 2013

    Phonegap photoswipe with download option script



    (function(window, PhotoSwipe){

    document.addEventListener('DOMContentLoaded', function(){

    var
    options = {

    getImageCaption: function(el){

    var captionText, captionEl, captionTextSRC;

    // Get the caption from the alt tag
    if (el.nodeName === "IMG"){
    captionText = el.getAttribute('alt');
    captionTextSRC = el.getAttribute("src");
    }
    var i, j, childEl;
    for (i=0, j=el.childNodes.length; i childEl = el.childNodes[i];
    if (el.childNodes[i].nodeName === 'IMG'){
    captionText = childEl.getAttribute('alt');
    captionTextSRC = childEl.getAttribute("src");
    }
    }




    // Return a DOM element with custom styling
    captionEl = document.createElement('div');
    captionEl.style.cssText = 'background: red; font-weight: bold; padding: 5px;';
    captionEl.appendChild(document.createTextNode("Download"));
    //console.log('JJJJJJJJJJJJ'+ captionTextSRC);
    //captionEl.onclick = testing;
    //var fileTransfer = new FileTransfer();
    captionEl.onclick = (function() {
    //navigator.notification.alert("Loasdasdsaddsdsdsdgin Successfull");
    var fileTransfer = new FileTransfer();
    fileTransfer.download(
        captionTextSRC,
        "file://sdcard/"+captionText,
        function(entry) {
           navigator.notification.alert("download complete: " + entry.fullPath);
        },
        function(error) {
           navigator.notification.alert("download error source " + error.source);
            navigator.notification.alert("download error target " + error.target);
            navigator.notification.alert("upload error code" + error.code);
        });

    });


    //captionEl.appendChild(document.createTextNode(captionText));
    return captionEl;

    // Alternatively you can just pass back a string. However, any HTML
    // markup will be escaped

    }

    },
    instance = PhotoSwipe.attach( window.document.querySelectorAll('#Gallery a'), options );

    }, false);


    }(window, window.Code.PhotoSwipe));


    function testing()
    {
    navigator.notification.alert("Login Successfull");
    }

    Wednesday, 13 March 2013

    SVN: Authorisation Failed

    Use the below command: rm ~/.gnome2/keyrings/login.keyring

    Monday, 2 July 2012

    How to get the Spanish Character from URL in php?

    How to get the Spanish Character from URL in php?

    Use utf8_decode("string");

    Tuesday, 15 November 2011

    set_time_limit in php, set_time_limit in php5, why set_time_limit?

    WHY set_time_limit ?
    It is used to run the scripts in the file for a particular amount of time. If we just put set_time_limit(1) then the setting will allow the file to run the script for 1 second and if the time exceeds then it will throw the fatal error

    Set_time_limit(0) – No time limit will be set, but I didt checked my MAX_EXECUTION_TIME, if there is still problem in the set_time_limit please check with the MAX_EXECUTION_TIME in the php.ini settings.

    Let us check some examples
    set_time_limit(0);
    function ran()
    {
    $i=100000;
    for ($j=0;$j<$i;$j++)
    {
    echo $j;
    }
    }
    $gg=ran();
    ?>
    >>>>> WONT THROW ANY ERROR


    set_time_limit(1);
    function ran()
    {
    $i=100000;
    for ($j=0;$j<$i;$j++)
    {
    echo $j;
    }
    }
    $gg=ran();
    ?>

    >>>>>> HERE WE SET THIS TO 1 SECOND BUT THE SCRIPT NEEDS TO RUN A LOOP FOR 100000 SO IT WILL THROW THE ERROR LIKE BELOW
    Fatal error: Maximum execution time of 1 second exceeded in ------------------------------------------------------------------------