<?php
/**
 * Script para gerar url de vídeos do igtv
 * Aplicações:
 * - Baixar videos
 * - Exibir video em player próprio (jwplayer)
 * @link http://mndti.com
 * @author ThiagoInfo
 * @version v2
 * @example $video = carrega_dados(url do video do ig completa)
*/ 

//----------------------------------------------------------------------------
// FUNÇÃO CURL PARA URL FILE DISABLE - SIMPLE XML LOAD
//----------------------------------------------------------------------------

    function formata_url($url){
        $split = preg_split('/[-.]/', $url);
        return $split[4];
    }	
	
 	function curl($url) {
		$ch = curl_init();
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_HEADER, 0);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);		
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
		$data = curl_exec($ch);
		curl_close($ch);
    		
		return $data;
    }
    // Verifico se existe no servidor a função curl.
    function carrega_dados ($url) {
        $vid = formata_url($url);
        $url = 'http://player.tvig.ig.com.br/api/2c07e4149695bbb43990fa7a9e2ed2ee/'.$vid.'?cache=&callback=jsonp_'.$vid.'&profileURL=http://player.tvig.ig.com.br/current/profiles/ig/&profileName=sambaPlayer.xml';
          	if (ini_get('allow_url_fopen')){
    		$json = file_get_contents($url);
    	} else {
    		$json = $this->curl($url);
    	}
        $rep = array("jsonp_{$vid}(", ')');
        $sub = array('','');
        $json = str_replace($rep, $sub, $json);

    	return json_decode($json);
    }
   $video = carrega_dados('http://tvig.ig.com.br/noticias/tecnologia/aviao+movido+a+energia+solar+faz+1++voo+internacional-8a4980262fc651e4012ff9e56a5a1007.html');
   
   var_dump($video->outputList[0]->url);
?>  