Accueil > > > GDATA CLASS
GDATA CLASS
Information sur la source
Description
Cette classe permet de controler de toute sorte de document ou dossier sur google docs, c'est à dire creer, effacer, upload et download ... etc.
Source
- <?php
-
- /**
-
- * PHP gData
-
- *
-
- * For support issues please contact me by email :
-
- * darkinux@gmail.com
-
- * or the official web site:
-
- * http://www.boxcreation.com
-
- * ==============================================================================
-
- *
-
- * @version $Id: gdata.class.php,v 0.5 2010/06/20 10:54:32 $
-
- * @copyright Copyright (c) 2007 Yassine Oussi (http://www.boxcreation.com)
-
- * @author Yassine Oussi <darkinux@gmail.com>
-
- * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
-
- *
-
- * ==============================================================================
-
- */
-
-
-
- class gData
- {
-
- private $username;
- private $password;
- private $auth;
- private $curl;
- private $status ="not connected";
-
- //--Public methods--//
-
-
-
-
- /**
- * Class constructor
- * Set auth to google Docs
- * @param $email, $password
- * @access public
- * @return void
- */
-
- public function __construct($username, $password)
- {
- $this->username = $username;
- $this->password = $password;
-
- // Construct an HTTP POST request
- $clientlogin_url = "https://www.google.com/accounts/ClientLogin";
- $clientlogin_post = array("accountType" => "HOSTED_OR_GOOGLE",
- "Email" => $this->username,
- "Passwd" => $this->password,
- "service" => "writely",
- "source" => "Gdata");
-
- // Initialize the curl object
- $this->curl = curl_init($clientlogin_url);
-
- // Set some options (some for SHTTP)
- curl_setopt($this->curl, CURLOPT_POST, true);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, $clientlogin_post);
- curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
-
- // Execute
- $response = curl_exec($this->curl);
-
- // Get the Auth string and save it
- if(preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches))
- {
- $this->auth = $matches[1];
- $this->status = "connected";
- }
- else
- {
- preg_match("/Error=([a-z0-9_\-]+)/i", $response, $matches);
- $this->status = "not connected : ". $matches[1] ." ". curl_error($this->curl);
- }
- }
-
-
-
- /**
- * getStatus
- * check the status
- * @access public
- * @return String
- */
-
- public function getStatus()
- {
- return $this->status;
- }
-
-
-
- /**
- * createFolder
- * Create a folder or subfolder
- * @param $name, $idFolder=0
- * @access public
- * @return array
- */
-
- public function createFolder($name, $idFolder = NULL)
- {
- // Include the Auth string in the headers
- // Together with the API version being used
- $headers = array(
- "Authorization: GoogleLogin auth=" . $this->auth,
- "GData-Version: 3.0",
- "Content-Type: application/atom+xml");
- if($idFolder)
- {
- $url = "http://docs.google.com/feeds/default/private/full/folder%3A". $idFolder ."/contents";
- }
- else
- {
- $url = "http://docs.google.com/feeds/default/private/full";
- }
-
- $xmlstr = "<?xml version='1.0' encoding='UTF-8'?>
- <atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\">
- <atom:category scheme=\"http://schemas.google.com/g/2005#kind\"
- term=\"http://schemas.google.com/docs/2007#folder\" label=\"folder\"/>
- <atom:title>". $name ."</atom:title>
- </atom:entry>";
-
- // Make the request
- curl_setopt($this->curl, CURLOPT_URL, $url);
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($this->curl, CURLOPT_POST, true);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, $xmlstr);
- curl_setopt($this->curl, CURLOPT_VERBOSE, true);
- curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
-
- // Execute
- $response = curl_exec($this->curl);
-
- // Get error
- if(curl_errno($this->curl))
- throw new Exception(curl_error($this->curl));
-
- $response = simplexml_load_string($response);
-
- if(preg_match("/folder%3A([a-z0-9_\-]+)/i",$response->id[0], $matches))
- {
- $arr["name"] = $response->title;
- $arr["type"] = $response->content["type"];
- $arr["down"] = $response->content["src"];
- $arr["link"] = $response->link[2]["href"];
- $arr["id"] = $matches[1];
-
- return $arr;
- }
- }
-
-
-
- /**
- * getInfoFile
- * Remove a folder
- * @param $idFolder, $next
- * @access public
- * @return array
- */
-
- public function getInfoFiles($idFolder = NULL) {
-
- // Include the Auth string in the headers
- // Together with the API version being used
- $headers = array(
- "Authorization: GoogleLogin auth=" . $this->auth,
- "GData-Version: 3.0",
- );
-
- $url = "http://docs.google.com/feeds/default/private/full";
-
- // Make the request
- curl_setopt($this->curl, CURLOPT_URL, $url);
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($this->curl, CURLOPT_VERBOSE, true);
- curl_setopt($this->curl, CURLOPT_POST, false);
-
- // Execute
- $response = curl_exec($this->curl);
-
- // Get error
- if(curl_errno($this->curl))
- throw new Exception(curl_error($this->curl));
-
- // Parse the response
- $response = simplexml_load_string($response);
-
- // Get files
- $size = sizeOf($response);
- for ($i=0; $i<$size; $i++)
- {
- if($response->entry[$i]->title && preg_match("/%3A([a-z0-9_\-]+)/i", $response->entry[$i]->link[2]["href"], $matches))
- {
- $arr[$i]["name"] = $response->entry[$i]->title ;
- $arr[$i]["type"] = $response->entry[$i]->content["type"];
- $arr[$i]["down"] = $response->entry[$i]->content["src"];
- $arr[$i]["link"] = $response->entry[$i]->link[2]["href"];
- $arr[$i]["author"] = $response->entry[$i]->author->name;
- $arr[$i]["id"] = $matches[1];
- }
- }
- return $arr;
- }
-
-
-
- /**
- * getFolders
- * get a list of folder
- * @access public
- * @return array
- */
-
- public function getInfoFolders() {
-
- // Include the Auth string in the headers
- // Together with the API version being used
- $headers = array(
- "Authorization: GoogleLogin auth=" . $this->auth,
- "GData-Version: 3.0",
- );
-
- $url = "http://docs.google.com/feeds/default/private/full/-/folder";
-
- // Make the request
- curl_setopt($this->curl, CURLOPT_URL, $url);
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($this->curl, CURLOPT_VERBOSE, true);
- curl_setopt($this->curl, CURLOPT_POST, false);
-
- // Execute
- $response = curl_exec($this->curl);
-
- // Get error
- if(curl_errno($this->curl))
- throw new Exception(curl_error($this->curl));
-
- // Parse the response
- $response = simplexml_load_string($response);
-
- // Get folders
- $size = sizeOf($response);
- for ($i=0; $i<$size; $i++)
- {
- if($response->entry[$i]->title && preg_match("/folder%3A([a-z0-9_\-]+)/i", $response->entry[$i]->content[0]["src"], $matches))
- {
- $arr[$i]["name"] = $response->entry[$i]->title;
- $arr[$i]["link"] = $response->entry[$i]->content["src"];
- $arr[$i]["author"] = $response->entry[$i]->author->name;
- $arr[$i]["id"] = $matches[1];
- }
- }
-
- return $arr;
- }
-
-
-
- /**
- * uploadFile
- * Upload file
- * @param $file, $name, $idFolder
- * @access public
- * @return array
- */
-
- public function uploadFile($file, $name, $idFolder = null)
- {
-
- // Include the Auth string in the headers
- // Together with the API version being used
- if(!@filesize($file))
- {
- throw new Exception("File not found!");
- }
- else
- {
-
- $headers = array(
- "Authorization: GoogleLogin auth=" . $this->auth,
- "GData-Version: 3.0",
- "Content-Length: ". filesize($file),
- "Content-Type: ". $this->returnMIMEType($file),
- "Slug: ". $name);
-
-
- if($idFolder)
- {
- $url = "http://docs.google.com/feeds/default/private/full/folder%3A". $idFolder ."/contents";
- }
- else
- {
- $url = "http://docs.google.com/feeds/default/private/full";
- }
-
- // Make the request
- curl_setopt($this->curl, CURLOPT_URL, $url);
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($this->curl, CURLOPT_POST, true);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, file_get_contents($file));
- curl_setopt($this->curl, CURLOPT_VERBOSE, true);
-
- // Execute
- $response = curl_exec($this->curl);
-
- // Get error
- if(curl_errno($this->curl))
- throw new Exception(curl_error($this->curl));
-
- // Parse the response
- $response = simplexml_load_string($response);
-
- if(preg_match("/%3A([a-z0-9_\-]+)/i", $response->link[2]["href"], $matches))
- {
- $arr["name"] = $response->title;
- $arr["type"] = $response->content["type"];
- $arr["down"] = $response->content["src"];
- $arr["link"] = $response->link[2]["href"];
- $arr["id"] = $matches[1];
- return $arr;
- }
- else
- {
- return false;
- }
- }
- }
-
-
-
- /**
- * getFile
- * Download file from google docs
- * @param $id, $filename, $format
- * @access public
- * @return array
- */
-
- public function getFile($link, $filename, $format)
- {
- // Include the Auth string in the headers
- // Together with the API version being used
- $headers = array(
- "Authorization: GoogleLogin auth=" . $this->auth,
- "GData-Version: 3.0",
- );
-
- // Make the request
- curl_setopt($this->curl, CURLOPT_HTTPGET, true);
- curl_setopt($this->curl, CURLOPT_URL, $link ."&exportFormat=". $format);
-
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($this->curl, CURLOPT_VERBOSE, true);
- curl_setopt($this->curl, CURLOPT_HEADER, false);
-
- // Execute
- $response = curl_exec($this->curl);
-
- // Get error
- if(curl_errno($this->curl))
- throw new Exception(curl_error($this->curl));
-
- // save the file
- $out = fopen($filename, 'wb');
- fwrite($out, $response);
- fclose($out);
-
- $arr["filename"] = $filename;
- $arr["content"] = $this->returnMIMEType($filename);
-
- return $arr;
- }
-
-
-
- /**
- * removeFile
- * Remove a file
- * @param $idFile
- * @access public
- * @return void
- */
-
- public function removeFileFolder($idFile)
- {
- // Include the Auth string in the headers
- // Together with the API version being used
- $headers = array(
- "GData-Version: 3.0",
- "If-Match: * ",
- "Authorization: GoogleLogin auth=" . $this->auth,
- );
-
- // Make the request
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
- curl_setopt($this->curl, CURLOPT_URL, "http://docs.google.com/feeds/default/private/full/". $idFile ."?delete=true");
- curl_setopt($this->curl, CURLOPT_VERBOSE, true);
- curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, false);
- curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
-
- // Execute
- curl_exec($this->curl);
-
- // Get error
- if(curl_errno($this->curl))
- throw new Exception(curl_error($this->curl));
- }
-
-
-
- /**
- * moveFileFolder
- * move file to Folder
- * @param $id, $idFolder, $type
- * @access public
- * @return void
- */
-
- public function moveFileToFolder($id, $idFolder, $type="file")
- {
- // Include the Auth string in the headers
- // Together with the API version being used
- $headers = array(
- "Authorization: GoogleLogin auth=" . $this->auth,
- "GData-Version: 3.0",
- "Content-Type: application/atom+xml");
-
- if($type == "file")
- {
- $xmlstr = "<?xml version='1.0' encoding='UTF-8'?>
- <entry xmlns=\"http://www.w3.org/2005/Atom\">
- <id>https://docs.google.com/feeds/default/private/full/document%3A". $id ."</id>
- </entry>";
- }
- elseif($type == "folder")
- {
- $xmlstr = "<?xml version='1.0' encoding='UTF-8'?>
- <entry xmlns=\"http://www.w3.org/2005/Atom\">
- <id>https://docs.google.com/feeds/default/private/full/folder%3A". $id ."</id>
- </entry>";
-
- }
-
- // Make the request
- curl_setopt($this->curl, CURLOPT_URL, "http://docs.google.com/feeds/default/private/full/folder%3A". $idFolder ."/contents");
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($this->curl, CURLOPT_POST, true);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, $xmlstr);
- curl_setopt($this->curl, CURLOPT_VERBOSE, true);
- curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
-
- // Execute
- curl_exec($this->curl);
-
- // Get error
- if(curl_errno($this->curl))
- throw new Exception(curl_error($this->curl));
- }
-
-
-
-
- /**
- * closeConnection
- * Curl close
- * @access public
- * @return void
- */
-
- public function closeConnection()
- {
- curl_close($this->curl);
- }
-
-
-
- //--Private methods--//
-
- /**
- * returnMIMEType
- * Return mime type
- * @param $filename
- * @access private
- * @return string
- */
-
- private function returnMIMEType($filename)
- {
- preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);
-
- switch(strtolower($fileSuffix[1]))
- {
- case "js" :
- return "application/x-javascript";
-
- case "json" :
- return "application/json";
-
- case "jpg" :
- case "jpeg" :
- case "jpe" :
- return "image/jpg";
-
- case "png" :
- case "gif" :
- case "bmp" :
- case "tiff" :
- return "image/".strtolower($fileSuffix[1]);
-
- case "css" :
- return "text/css";
-
- case "xml" :
- return "application/xml";
-
- case "doc" :
- case "docx" :
- return "application/msword";
-
- case "xls" :
- case "xlt" :
- case "xlm" :
- case "xld" :
- case "xla" :
- case "xlc" :
- case "xlw" :
- case "xll" :
- return "application/vnd.ms-excel";
-
- case "ppt" :
- case "pps" :
- return "application/vnd.ms-powerpoint";
-
- case "rtf" :
- return "application/rtf";
-
- case "pdf" :
- return "application/pdf";
-
- case "html" :
- case "htm" :
- case "php" :
- return "text/html";
-
- case "txt" :
- return "text/plain";
-
- case "mpeg" :
- case "mpg" :
- case "mpe" :
- return "video/mpeg";
-
- case "mp3" :
- return "audio/mpeg3";
-
- case "wav" :
- return "audio/wav";
-
- case "aiff" :
- case "aif" :
- return "audio/aiff";
-
- case "avi" :
- return "video/msvideo";
-
- case "wmv" :
- return "video/x-ms-wmv";
-
- case "mov" :
- return "video/quicktime";
-
- case "zip" :
- return "application/zip";
-
- case "tar" :
- return "application/x-tar";
-
- case "swf" :
- return "application/x-shockwave-flash";
-
- default :
- if(function_exists("mime_content_type"))
- {
- $fileSuffix = mime_content_type($filename);
- }
-
- return "unknown/" . trim($fileSuffix[0], ".");
- }
- }
-
- }
- ?>
<?php
/**
* PHP gData
*
* For support issues please contact me by email :
* darkinux@gmail.com
* or the official web site:
* http://www.boxcreation.com
* ==============================================================================
*
* @version $Id: gdata.class.php,v 0.5 2010/06/20 10:54:32 $
* @copyright Copyright (c) 2007 Yassine Oussi (http://www.boxcreation.com)
* @author Yassine Oussi <darkinux@gmail.com>
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*
* ==============================================================================
*/
class gData
{
private $username;
private $password;
private $auth;
private $curl;
private $status ="not connected";
//--Public methods--//
/**
* Class constructor
* Set auth to google Docs
* @param $email, $password
* @access public
* @return void
*/
public function __construct($username, $password)
{
$this->username = $username;
$this->password = $password;
// Construct an HTTP POST request
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array("accountType" => "HOSTED_OR_GOOGLE",
"Email" => $this->username,
"Passwd" => $this->password,
"service" => "writely",
"source" => "Gdata");
// Initialize the curl object
$this->curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
// Execute
$response = curl_exec($this->curl);
// Get the Auth string and save it
if(preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches))
{
$this->auth = $matches[1];
$this->status = "connected";
}
else
{
preg_match("/Error=([a-z0-9_\-]+)/i", $response, $matches);
$this->status = "not connected : ". $matches[1] ." ". curl_error($this->curl);
}
}
/**
* getStatus
* check the status
* @access public
* @return String
*/
public function getStatus()
{
return $this->status;
}
/**
* createFolder
* Create a folder or subfolder
* @param $name, $idFolder=0
* @access public
* @return array
*/
public function createFolder($name, $idFolder = NULL)
{
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
"Content-Type: application/atom+xml");
if($idFolder)
{
$url = "http://docs.google.com/feeds/default/private/full/folder%3A". $idFolder ."/contents";
}
else
{
$url = "http://docs.google.com/feeds/default/private/full";
}
$xmlstr = "<?xml version='1.0' encoding='UTF-8'?>
<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\">
<atom:category scheme=\"http://schemas.google.com/g/2005#kind\"
term=\"http://schemas.google.com/docs/2007#folder\" label=\"folder\"/>
<atom:title>". $name ."</atom:title>
</atom:entry>";
// Make the request
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $xmlstr);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
// Execute
$response = curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
$response = simplexml_load_string($response);
if(preg_match("/folder%3A([a-z0-9_\-]+)/i",$response->id[0], $matches))
{
$arr["name"] = $response->title;
$arr["type"] = $response->content["type"];
$arr["down"] = $response->content["src"];
$arr["link"] = $response->link[2]["href"];
$arr["id"] = $matches[1];
return $arr;
}
}
/**
* getInfoFile
* Remove a folder
* @param $idFolder, $next
* @access public
* @return array
*/
public function getInfoFiles($idFolder = NULL) {
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
);
$url = "http://docs.google.com/feeds/default/private/full";
// Make the request
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_POST, false);
// Execute
$response = curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
// Parse the response
$response = simplexml_load_string($response);
// Get files
$size = sizeOf($response);
for ($i=0; $i<$size; $i++)
{
if($response->entry[$i]->title && preg_match("/%3A([a-z0-9_\-]+)/i", $response->entry[$i]->link[2]["href"], $matches))
{
$arr[$i]["name"] = $response->entry[$i]->title ;
$arr[$i]["type"] = $response->entry[$i]->content["type"];
$arr[$i]["down"] = $response->entry[$i]->content["src"];
$arr[$i]["link"] = $response->entry[$i]->link[2]["href"];
$arr[$i]["author"] = $response->entry[$i]->author->name;
$arr[$i]["id"] = $matches[1];
}
}
return $arr;
}
/**
* getFolders
* get a list of folder
* @access public
* @return array
*/
public function getInfoFolders() {
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
);
$url = "http://docs.google.com/feeds/default/private/full/-/folder";
// Make the request
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_POST, false);
// Execute
$response = curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
// Parse the response
$response = simplexml_load_string($response);
// Get folders
$size = sizeOf($response);
for ($i=0; $i<$size; $i++)
{
if($response->entry[$i]->title && preg_match("/folder%3A([a-z0-9_\-]+)/i", $response->entry[$i]->content[0]["src"], $matches))
{
$arr[$i]["name"] = $response->entry[$i]->title;
$arr[$i]["link"] = $response->entry[$i]->content["src"];
$arr[$i]["author"] = $response->entry[$i]->author->name;
$arr[$i]["id"] = $matches[1];
}
}
return $arr;
}
/**
* uploadFile
* Upload file
* @param $file, $name, $idFolder
* @access public
* @return array
*/
public function uploadFile($file, $name, $idFolder = null)
{
// Include the Auth string in the headers
// Together with the API version being used
if(!@filesize($file))
{
throw new Exception("File not found!");
}
else
{
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
"Content-Length: ". filesize($file),
"Content-Type: ". $this->returnMIMEType($file),
"Slug: ". $name);
if($idFolder)
{
$url = "http://docs.google.com/feeds/default/private/full/folder%3A". $idFolder ."/contents";
}
else
{
$url = "http://docs.google.com/feeds/default/private/full";
}
// Make the request
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, file_get_contents($file));
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
// Execute
$response = curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
// Parse the response
$response = simplexml_load_string($response);
if(preg_match("/%3A([a-z0-9_\-]+)/i", $response->link[2]["href"], $matches))
{
$arr["name"] = $response->title;
$arr["type"] = $response->content["type"];
$arr["down"] = $response->content["src"];
$arr["link"] = $response->link[2]["href"];
$arr["id"] = $matches[1];
return $arr;
}
else
{
return false;
}
}
}
/**
* getFile
* Download file from google docs
* @param $id, $filename, $format
* @access public
* @return array
*/
public function getFile($link, $filename, $format)
{
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
);
// Make the request
curl_setopt($this->curl, CURLOPT_HTTPGET, true);
curl_setopt($this->curl, CURLOPT_URL, $link ."&exportFormat=". $format);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_HEADER, false);
// Execute
$response = curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
// save the file
$out = fopen($filename, 'wb');
fwrite($out, $response);
fclose($out);
$arr["filename"] = $filename;
$arr["content"] = $this->returnMIMEType($filename);
return $arr;
}
/**
* removeFile
* Remove a file
* @param $idFile
* @access public
* @return void
*/
public function removeFileFolder($idFile)
{
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"GData-Version: 3.0",
"If-Match: * ",
"Authorization: GoogleLogin auth=" . $this->auth,
);
// Make the request
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($this->curl, CURLOPT_URL, "http://docs.google.com/feeds/default/private/full/". $idFile ."?delete=true");
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, false);
curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
// Execute
curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
}
/**
* moveFileFolder
* move file to Folder
* @param $id, $idFolder, $type
* @access public
* @return void
*/
public function moveFileToFolder($id, $idFolder, $type="file")
{
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
"Content-Type: application/atom+xml");
if($type == "file")
{
$xmlstr = "<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns=\"http://www.w3.org/2005/Atom\">
<id>https://docs.google.com/feeds/default/private/full/document%3A". $id ."</id>
</entry>";
}
elseif($type == "folder")
{
$xmlstr = "<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns=\"http://www.w3.org/2005/Atom\">
<id>https://docs.google.com/feeds/default/private/full/folder%3A". $id ."</id>
</entry>";
}
// Make the request
curl_setopt($this->curl, CURLOPT_URL, "http://docs.google.com/feeds/default/private/full/folder%3A". $idFolder ."/contents");
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $xmlstr);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
// Execute
curl_exec($this->curl);
// Get error
if(curl_errno($this->curl))
throw new Exception(curl_error($this->curl));
}
/**
* closeConnection
* Curl close
* @access public
* @return void
*/
public function closeConnection()
{
curl_close($this->curl);
}
//--Private methods--//
/**
* returnMIMEType
* Return mime type
* @param $filename
* @access private
* @return string
*/
private function returnMIMEType($filename)
{
preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);
switch(strtolower($fileSuffix[1]))
{
case "js" :
return "application/x-javascript";
case "json" :
return "application/json";
case "jpg" :
case "jpeg" :
case "jpe" :
return "image/jpg";
case "png" :
case "gif" :
case "bmp" :
case "tiff" :
return "image/".strtolower($fileSuffix[1]);
case "css" :
return "text/css";
case "xml" :
return "application/xml";
case "doc" :
case "docx" :
return "application/msword";
case "xls" :
case "xlt" :
case "xlm" :
case "xld" :
case "xla" :
case "xlc" :
case "xlw" :
case "xll" :
return "application/vnd.ms-excel";
case "ppt" :
case "pps" :
return "application/vnd.ms-powerpoint";
case "rtf" :
return "application/rtf";
case "pdf" :
return "application/pdf";
case "html" :
case "htm" :
case "php" :
return "text/html";
case "txt" :
return "text/plain";
case "mpeg" :
case "mpg" :
case "mpe" :
return "video/mpeg";
case "mp3" :
return "audio/mpeg3";
case "wav" :
return "audio/wav";
case "aiff" :
case "aif" :
return "audio/aiff";
case "avi" :
return "video/msvideo";
case "wmv" :
return "video/x-ms-wmv";
case "mov" :
return "video/quicktime";
case "zip" :
return "application/zip";
case "tar" :
return "application/x-tar";
case "swf" :
return "application/x-shockwave-flash";
default :
if(function_exists("mime_content_type"))
{
$fileSuffix = mime_content_type($filename);
}
return "unknown/" . trim($fileSuffix[0], ".");
}
}
}
?>
Conclusion
n'hesitez pas si vous avez des suggestions pour l'amélioration.
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Google agenda (Zend_Gdata) [ par Soulame ]
Bonjour, Je fais un site internet qui doit pouvoir créer un agenda, dans l'application google agenda, et le partager à un autre utilisateur (à la mai
Les testes que google organise pour php ou autre language ? [ par babare77 ]
Bonjour, j'ai entendu parlé des testes que google organise pour se tester dans un langage ! j'aimerai bien savoir quand ça se passe et comment m'inscr
Bloquer des caractères ou chaine [ par booba27100 ]
Bonjour, depuis peu, je me suis aperçu que dans ma zone de saisie sur ma page php, lorsque je tape une phrase, et que je click sur 'ajouter', elle s'a
Insertion d'une page externe à mon site [ par smoana ]
Bonjour, Voila je vous mets une partie de mon code en exemple. Pouvez vous me dire si cela peut fonctionner. <?php // On définit le tableau co
Google Maps API & PHP [ par begueradj ]
Bonjour, Peut-on utiliser Google Maps plusieurs fois sur son site par une même clé ?
Google Instant Preview [ par sachaaa ]
Bonjour je suis en train de me faire une page ou il y aura des liens et j'aimerais la faire comme sur GOOGLE avec son Instant Preview pour que les vi
[Zend Framework] Google API Calendar [ par bibi21700 ]
Bonjour à tous, je dois utiliser Zend Framework pour utiliser Google Calendar. Je suis pour le moment le tutorial [url=http://code.google.com/intl/fr/
[Zend Framework] Google API Calendar [ par bibi21700 ]
Bonjour à tous, je dois utiliser Zend Framework pour utiliser Google Calendar. Je suis pour le moment le [url=http://code.google.com/intl/fr/apis/cale
Réserver une plage avec google api agenda [ par bibi21700 ]
Bonjour, j'ai suivi le tutoriel sur [url=http://code.google.com/intl/fr/apis/calendar/data/1.0/developers_guide_php.html]le site[/url]. Je lance le fi
comment afficher Le texte descriptif et le titre sur Google [ par elmeksaoui1 ]
Bonjour, j ai héberger un site web et je vaudrais afficher Le texte descriptif et le titre sur moteurs de recherche Google lorsqu'on fait une recher
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|