PHP Classes

Trouble uploading files, can't seem to set RequestContentType

Recommend this page to a friend!

      PHP OAuth Library  >  PHP OAuth Library package blog  >  How to Implement a PH...  >  All threads  >  Trouble uploading files, can't seem...  >  (Un) Subscribe thread alerts  
Subject:Trouble uploading files, can't seem...
Summary:Unable to set RequestContentType option in order to upload files
Messages:3
Author:Meitar
Date:2016-01-23 05:16:38
 

  1. Trouble uploading files, can't seem...   Reply   Report abuse  
Picture of Meitar Meitar - 2016-01-23 05:16:38
Hi, I like your class a lot and have used it for a while, but am trying to do something new with it and I am running into trouble.

I am trying to upload files to WordPress.com via their REST API as described on this page: https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/media/new/

They describe a request format that requires setting a POST parameter array with the key "media" and indicate that the request type must be `multipart/form-data` encoded. I have tried a number of different code variations but am unable to make this class produce such a request. (Other requests work fine.)

My call to the class method in code looks something like this:

---BEGIN CODE--
error_log("\$url = $url");
error_log("\$method = $method");
error_log('$params = ' . print_r($params, true));
error_log('$opts = ' . print_r($opts, true));
$client->CallAPI('https://public-api.wordpress.com/rest/v1.1' . $url, $method, $params, $opts, $resp);

---END CODE---

and the debug log output looks like this:

--BEGIN LOG--

[23-Jan-2016 05:08:16] $url = /sites/meitardev.wordpress.com/media/new
[23-Jan-2016 05:08:16] $method = POST
[23-Jan-2016 05:08:16] $params = Array
(
[media] => Array
(
[0] => /media/sf_wordpress-vm/wp-content/uploads/2016/01/tumblr_moamd8m4sS1s1i9t5o1_500.jpg
)

)

[23-Jan-2016 05:08:16] $opts = Array
(
[Files] => Array
(
[Type] => FileName
[ContenType] => automatic/name
[media] => Array
(
)

)

[RequestContentType] => multipart/form-data
)

[23-Jan-2016 05:08:16] OAuth client: Accessing the API call at https://public-api.wordpress.com/rest/v1.1/sites/meitardev.wordpress.com/media/new
[23-Jan-2016 05:08:16] Connecting to public-api.wordpress.com
[23-Jan-2016 05:08:16] Resolving HTTP server domain "public-api.wordpress.com"...
[23-Jan-2016 05:08:16] Connecting to HTTP server IP 192.0.78.22 port 443...
[23-Jan-2016 05:08:16] Connected to public-api.wordpress.com
[23-Jan-2016 05:08:16] C POST /rest/v1.1/sites/meitardev.wordpress.com/media/new HTTP/1.1
[23-Jan-2016 05:08:16] C Host: public-api.wordpress.com
[23-Jan-2016 05:08:16] C User-Agent: PHP-OAuth-API (http://www.phpclasses.org/oauth-api $Revision: 1.93 $)
[23-Jan-2016 05:08:16] C Accept: */*
[23-Jan-2016 05:08:16] C Authorization: Bearer […REDACTED BUT VALID…]
[23-Jan-2016 05:08:16] C Connection: Keep-Alive
[23-Jan-2016 05:08:16] C Content-Type: application/x-www-form-urlencoded
[23-Jan-2016 05:08:16] C Content-Length: 104
[23-Jan-2016 05:08:16] C
[23-Jan-2016 05:08:16] C media=%2Fmedia%2Fsf_wordpress-vm%2Fwp-content%2Fuploads%2F2016%2F01%2Ftumblr_moamd8m4sS1s1i9t5o1_500.jpg
[23-Jan-2016 05:08:17] S HTTP/1.1 400 Bad Request
[23-Jan-2016 05:08:17] S Server: nginx
[23-Jan-2016 05:08:17] S Date: Sat, 23 Jan 2016 05:08:17 GMT
[23-Jan-2016 05:08:17] S Content-Type: application/json
[23-Jan-2016 05:08:17] S Transfer-Encoding: chunked
[23-Jan-2016 05:08:17] S Connection: keep-alive
[23-Jan-2016 05:08:17] S X-Prefork: 1
[23-Jan-2016 05:08:17] S X-hacker: Oh, Awesome: Opossum
[23-Jan-2016 05:08:17] S Expires: Wed, 11 Jan 1984 05:00:00 GMT
[23-Jan-2016 05:08:17] S Cache-Control: no-cache, must-revalidate, max-age=0
[23-Jan-2016 05:08:17] S Pragma: no-cache
[23-Jan-2016 05:08:17] S Access-Control-Allow-Origin: *
[23-Jan-2016 05:08:17] S X-ac: 2.bur _bur
[23-Jan-2016 05:08:17] S Strict-Transport-Security: max-age=15552000
[23-Jan-2016 05:08:17] S
[23-Jan-2016 05:08:17] S 76
[23-Jan-2016 05:08:17] S {"errors":[{"file":"\/","error":"upload_error","message":"File is empty. Please upload something more substantial."}]}
[23-Jan-2016 05:08:17] S 0
[23-Jan-2016 05:08:17] S

--END LOG--


Notice how the Content-Type of the request is still the default, despite my trying to set it explicitly.

I also read the docs but they are not perfectly clear to me. Is it the case that I *must* create the 'RequestBody' myself in order to change the content type? If so, what must I do to ensure that the parameters are handled correctly (or will they just be ignored if I create a custom request)?

The docs seem to suggest that it is possible to give the CallAPI() method the name of a file on the local disk and then have the library handle the upload correctly. What must I do to make that work?

Thank you in advance for any help you can offer to clarify this.

  2. Re: Trouble uploading files, can't seem...   Reply   Report abuse  
Picture of Meitar Meitar - 2016-01-23 05:59:34 - In reply to message 1 from Meitar
Oh, never mind, I realize I've been using an outdated version of the library. Indeed, this old version had a bug that checked the wrong variable before it added files to the request. Updating to the latest version of the library and re-checking my code worked.

Thank you again for continuing to support and improve a great lightweight library for OAuth interactions.

  3. Re: Trouble uploading files, can't seem...   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2016-01-23 19:24:32 - In reply to message 1 from Meitar
It seems the uploaded file is empty according to the error message.