I need convert html or docx use grive api.
My code
$client = new \Google_Client();
$client->setAccessToken($_SESSION['access_token']);
$service = new \Google_Service_Drive($client);
$content = file_get_contents('template.html');//template.docx
$fileMetadata = new \Google_Service_Drive_DriveFile(array(
'name' => 'test', // name file
'mimeType' => 'application/pdf', // mime type for save in gdrive
//'parents' => array($id_folder)
));
$file = $service->files->create($fileMetadata, array(
'data' => $content, // file
'mimeType' => 'text/html', // application/vnd.openxmlformats-officedocument.wordprocessingml.document
//'uploadType' => 'multipart',
'fields' => 'id')); //
File upload to dgrive normal but when I open it test.pdf i see this error
Failed to load other pages
But my file have only one page.
How can I solve this problem?
See the examples here. In Particular I dont think you need to have fields element in there which i cant find in documentation. When you upload file as text/html make sure you have correct meta data and enable multi-part as you are doing both content and meta data in one go.
I would also suggest doing it in small steps and first let it be very simple html content and try that if that works it might be the case with your content that is not letting pdf come out as normal.
If you work through some of these examples then it would be easy to debug if the problem is in meta data, content or anything else.
Related
We are having an issue with sending MMS with PNG images. We previously use that and all works well now it failed.
The code example is here:
$client = new Client(env('TWILIO_SID'), env('TWILIO_TOKEN'));
$message = $client->messages->create(
$number,
[
'from' => env('TWILIO_FROM'),
'body' => $content,
"statusCallback" => "https://OUR_URL.com/api/twilio_web_hooks",
"mediaUrl" => $image_url,
]
);
We get this error:
12300 Twilio is unable to process the Content-Type of the provided URL. Please see Twilio's documentation on accepted content types for more information on valid Content-Types.
Use a HEX editor and see if you see PNG towards the top.
PNG files start with 'PNG', . jpg files should have 'exif'or 'JFIF' somewhere in the beginning.
I'm trying to upload files to Google Drive but with the attribute convert=true to allow a automatic conversion of upload file to Google Docs format...but it does not work:
$drive_service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->setTitle("Informe-imaxbd-alfa");
$result = $drive_service->files->insert($file, array(
'data' => file_get_contents("notas-gdrive.docx"),
'mimeType' => 'application/octet-stream',
'uploadType' => 'multipart',
'convert' => FALSE,
));
The file is properly uploaded but without any conversion, why?
You are correct, that the proper mime type Google Drive API accepts is application/msword. I also found out as of writing this, Google Drive API does not support Word .docx format conversion for some reason.
So make sure that not only your extension is in .doc format, but actual document content is also older .doc (Word 97 - 2004) format.
I have created a simple PHP script to play around with Google's Drive SDK. The plan is that eventually we will use Google Drive as a form of CDN for some of our web content (our company already has already upgraded to 1TB).
The code works to a degree, in that it successfully authenticates and uploads a file. The problem is, the file is always broken and cannot be viewed either with Drive itself, or by downloading.
The code is relatively simple, and just fetches an image from Wikipedia and attempts an upload:
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
require_once 'Google/Service/Oauth2.php';
$client = new Google_Client();
//$client->setUseObjects(true);
//$client->setAuthClass('apiOAuth2');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
$client->setClientId('***');
$client->setClientSecret('***');
$client->setRedirectUri('***');
$client->setAccessToken(authenticate($client));
// initialise the Google Drive service
$service = new Google_Service_Drive($client);
$data = file_get_contents('http://upload.wikimedia.org/wikipedia/commons/3/38/JPEG_example_JPG_RIP_010.jpg');
// create and upload a new Google Drive file, including the data
try
{
//Insert a file
$file = new Google_Service_Drive_DriveFile($client);
$file->setTitle(uniqid().'.jpg');
$file->setMimeType('image/jpeg');
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
));
}
catch (Exception $e)
{
print $e->getMessage();
}
print_r($createdFile);
?>
The print_r statement executes and we get information about the file. However, as I mentioned, the file is not viewable, and appears to be corrupt. Can anyone shed any light on what the issue may be?
After doing some more digging around in the docs (the current public docs are seriously out of date), I found that it's necessary to send another parameter as part of the insert() function's body parameter (the second argument in the function call).
Using the following:
$createdFile = $service->files->insert($doc, array(
'data' => $content,
'mimeType' => 'image/jpeg',
'uploadType' => 'media', // this is the new info
));
I was able to get everything working. I'll leave the question here, as I think it will be very useful until such a time that Google actually updates the documentation for the PHP API.
Source info here
Your code seems to be ok.
Have You tried to download this file from Google Drive afterwards and look at it?
Also I know its abit stupid but have You tried to write file on disk right after using file_get_contents(). You know just to establish point where it goes bad for 100%.
Here is my code to upload a file:
private function addFile(File $file, $folderProject, User $user) {
$fileToUpload = new Google_DriveFile();
$fileToUpload->setTitle($file->getName());
//set mime type
$fileToUpload->setMimeType($file->getMimeType());
//build parent and set id
$parent = new Google_ParentReference();
$parent->setId($folderProject->getId());
//set parent to file
$fileToUpload->setParents(array($parent));
$data = file_get_contents($file->getPath());
$createdFile = $this->service->files->insert($fileToUpload, array(
'data' => $data,
'mimeType' => $file->getMimeType(),
));
}
When I upload for example a docx using this PHP code the file append to be unreadable on the Google Drive website (it shows up the XML structure of the docx) and it is showing up like this in my Google Drive folder:
And when i try to upload a docx directly through Google Drive UI it showing up like this (what i want):
I'm pretty sure that the problem is coming from the fact the Google
Drive UI tries to open the file in a different way because i uploaded
it through my app, but i don't want that :)
(i have the same problem with other kind of files)
I tried to set the parameter 'convert' = true in the array but then i got this error during the upload:
..convert=true&uploadType=multipart&key=AI39si49a86oqv9fhb9yzXp7FCWL-cSqwFiClHZqS3Y3r9Hw8ujtSNlZEWZyFKBp_id7LUyFaZTJ97pMeb0XqHznycK7JJ9o_Q:
(500) Internal Error
Documentation: https://developers.google.com/drive/v2/reference/files/insert
=> But anyway i don't think to convert is the right solution, i just want to be able to see the content as if i uploaded the file through the Google Drive UI
If i open the docx from my Google Drive Sync folders and I edit and save it, I will be able to see it through the Google Drive UI (showing with the W logo)
I also getting the same problem.
But the difference is that , I am uploading pdf and able to view pdf in google drive UI.
I need to automatically convert it to google doc format by api code after adding new pdf.
If I change $mimeType = 'application/vnd.google-apps.document'; then got below error
https://www.googleapis.com/upload/drive/v2/files?convert=true&uploadType=multipart: (400) Bad Request
if I add optional parameter
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $mimeType,
'convert' => TRUE
));
Then no change , only pdf upload no conversion takes place.
do u have any suggestion , where I need to exactly add for convert parameter ?
I am using amazon S3 service with PHP by using this API
https://github.com/tpyo/amazon-s3-php-class
I am passing the url to client like this
https://domain.s3.amazonaws.com/bucket/filename_11052011111924.zip?AWSAccessKeyId=myaccesskey&Expires=1305311393&Signature=mysignature
So when the client clicks or paste the URL into browser , the file downloaded with the name of filename_11052011111924.zip.But I stored my original filename in DB.
So is it possible to download when passing the URL alone to the client and download with original file name.I am not sure whether this will help me.
Content-Disposition: attachment; filename=FILENAME.EXT
Content-Type: application/octet-stream
If you set the headers that you listed on your file when you upload it to S3, you will be able to download the file with the original filename. (you can also set these on existing files in S3 - see the AWS docs)
I'm not sure if your library supports this but you can do it with the AWS S3 SDK.
Something like (I don't know php so check the syntax):
// Instantiate the class
$s3 = new AmazonS3();
$response = $s3->create_object('bucket', 'filename_11052011111924.zip', array(
'fileUpload' => 'filename.zip',
'contentType' => 'application/octet-stream',
'headers' => array( // raw headers
'Content-Disposition' => 'attachment; filename=filename.zip',
),
));
Update
You can also adjust certain headers each time you generate a new url. See http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#m=AmazonS3/get_object_url
$url = $s3->get_object_url('bucket', 'filename_11052011111924.zip', '5 minutes', array(
'response' => array(
'Content-Disposition' => 'attachment; filename=filename.zip'
)
));
I don't think that will work (I never tried it though). You might need to download the file to your server first, later use headers, once it is completed (or after sometime later with some bot or cron) you can delete the file(s).
This approach will be using your bandwidth.
Yes, you can tell to AWS how output file must be named:
Note: we encode file name!
$filename = "Here we can have some utf8 chars.ext";
$outputFileName = '=?UTF-8?B?' . base64_encode($filename) . '?=';
$url = $s3->get_object_url(
'bucket_name',
'path_to_the_file.ext',
'5 minutes',
array(
'response' => array(
'content-disposition' => 'attachment;' . " filename=\"" . $outputFileName . "\";")
)
)
);