Uno de los problemas con los que nos podemos encontrar es tener un documento en un formato y tener que exportarlo en otro formato. La exportación se puede realizar utilizando Google Docs, quizás un tanto rebuscada la solución, aunque quizás no tanto.
Os paso un script que sube el fichero a Google Docs dentro de una carpeta y acto seguido lo exporta a otro formato, en este caso subo un PPT y lo convierto en PDF (me hubiese encantado que fuera a HTML pero no acepta esa opción).
// Datos de login a la API de Google
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "GOOGLE",
"Email" => "miemail@gmail.com",
"Passwd" => "mipassword",
"service" => "writely",
"source" => "WPDOCS"
);
// Inicializamos el CURL
$curl = curl_init($clientlogin_url);
// Obtenemos el string de autenticación
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches);
$auth = $matches[1];
// Cabeceras de autenticación
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
// Recuperamos los ficheros y carpetas que tenemos en Google Docs para no crear dos veces la misma carpeta
curl_setopt($curl, CURLOPT_URL, "http://docs.google.com/feeds/default/private/full?showfolders=true");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, false);
$listado = curl_exec($curl);
$nombre_carpeta = 'WPDOCS';
// Si no se ha creado la carpeta, la creamos
if (strpos($listado, ''.$nombre_carpeta.'') === FALSE) {
// Make the request
$h = array_merge($headers,array('Content-Type: application/atom+xml'));
$xml = ''.$nombre_carpeta.'';
curl_setopt($curl, CURLOPT_URL, "http://docs.google.com/feeds/default/private/full");
curl_setopt($curl, CURLOPT_HTTPHEADER, $h);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_POST, true);
$response = curl_exec($curl);
$response = simplexml_load_string($response);
$id_folder = $response->id;
} else {
// Recuperamos la ID de la carpeta creada anteriormente
preg_match("#$nombre_carpetaid;
// Limpiamos los IDs de los ficheros devueltos por Google, solo nos interesa del %3A para adelante
preg_match('/%3A(.+)/', $id_doc, $m);
$id_doc = $m[1];
preg_match('/%3A(.+)/', $id_folder, $m);
$id_folder = $m[1];
// Lo movemos a la carpeta
$h = array_merge($headers,array('Content-Type: application/atom+xml'));
$data = 'https://docs.google.com/feeds/default/private/full/document%3A'.$id_doc.'';
curl_setopt($curl, CURLOPT_URL, "http://docs.google.com/feeds/default/private/full/folder%3A".$id_folder);
curl_setopt($curl, CURLOPT_HTTPHEADER, $h);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_POST, true);
$response = curl_exec($curl);
//header('Content-type: text/xml');
//echo $response;
// Parse the response
// Exportamos a HTML
curl_setopt($curl, CURLOPT_URL, "http://docs.google.com/feeds/download/presentations/Export?docID=$id_doc&exportFormat=pdf");
curl_setopt($curl, CURLOPT_HTTPHEADER, $h);
curl_setopt($curl, CURLOPT_POST, false);
header('Content-type: application/pdf');
echo curl_exec($curl);
curl_close($curl);
Vía / Google Docs API: Client Login with PHP and Curl
Yo uso otro funcion como reemplazo de var_dump (extraida de CakePHP) que aunque no tiene colores presenta la informacion bastante ordenada:
[code]
function debug($var = false, $showFrom = true) {
if ($showFrom) {
$calledFrom = debug_backtrace();
$file = str_replace(‘\\’, ‘/’, $calledFrom[0][‘file’]);
echo ‘‘ . $file . ‘‘;
echo ‘ (line ‘ . $calledFrom[0][‘line’] . ‘)’;
}
echo “\n\n”;
$var = print_r($var, true);
echo $var . “\n\n”;
}
[/code]
Bueno la clase esta interesante y muy practica, gracias por publicar un post con la info.
Saludos.
Gracias Gabriel por el script.
Domingo, espero que te sea de utilidad
Yo suelo utilizar la función fb de FirePHP, un complemento de Firebug:
http://www.firephp.org/