1

Can we upload files by creating a request from PHP with out using the google api library ?

https://developers.google.com/drive/api/guides/manage-uploads#http_1

2 Answers2

0

Yes you can, it's just HTTP requests. But it's likely to be simpler to use the library.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Thanks I am having hard time creating the request , could not find any example any where. – lukman nakib Jun 07 '22 at 10:31
  • I mean a complete request body with a demo file without using the library – lukman nakib Jun 07 '22 at 10:47
  • Lots of files I do not want to add a whole library for only one upload , I have found a python implementation https://stackoverflow.com/questions/10317638/how-do-i-add-create-insert-files-to-google-drive-through-the-api, but still confused This is part I am confused : Create the body of the request. Format the body according to the multipart/related content type [RFC 2387], which contains two parts: .. – lukman nakib Jun 07 '22 at 11:18
  • `Lots of files I do not want to add a whole library for only one upload`...lots of files which you don't have to maintain. And if you use a package manager like composer (which most people should, these days), then you really don't have to do anything except install it and ignore it, and just use the bits you need. It won't slow down your application or anything like that. IMO "Lots of files" isn't a reason to cause yourself lots of trouble trying to re-write functionality which someone else has already created for you! – ADyson Jun 07 '22 at 11:22
  • `This is part I am confused : Create the body of the request. Format the body according to the multipart/related content type [RFC 2387], which contains two parts:`...in what specific way are you confused about it? have you tried to write some code to implement it? Do you understand the structure of a HTTP request to begin with? Have you looked for existing examples of a multi-part request written in PHP? It's unclear what the actual problem is with that. – ADyson Jun 07 '22 at 11:24
0

The google drive api is a simple rest api. As long as you can send a http post you will be able to upload the file.

POST https://www.googleapis.com/drive/v3/files?key=[YOUR_API_KEY] HTTP/1.1

Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Content-Type: application/json

{body}

There is some documentation on how to format the file for upload manage-uploads#http Pay close attention to the Content-Type for the file meta data, As well as how to format the metadata for the file itself. You may want to rip a bit of code out of the client library to start with MediaFileUpload.php

The tricky part will be your authorization. You need to be sure to send a valid access token with every request. Oauth2 authorization is in the form of three requests.

  1. Request consent -> results in authorization code
  2. Exchange authorization code -> results in access token and refresh token
  3. Exchange refresh token for new access token.

More information can be found here web-server#httprest

I strongly suggest that you go with the PHP client library.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449