@@ -260,9 +260,6 @@ public function __call($fn, $params)
260
260
$ httpmethod = $ this ->_detectMethod ($ method_template , $ apiparams );
261
261
$ multipart = $ this ->_detectMultipart ($ method_template );
262
262
263
- // geek-geek: Now allowing to specify filenames as params
264
- $ this ->_detectFilenames ($ method_template , $ apiparams );
265
-
266
263
return $ this ->_callApi (
267
264
$ httpmethod ,
268
265
$ method ,
@@ -693,14 +690,15 @@ protected function _detectMultipart($method)
693
690
}
694
691
695
692
/**
696
- * Detects filenames in upload parameters
693
+ * Detect filenames in upload parameters,
694
+ * build multipart request from upload params
697
695
*
698
- * @param string $method The API method to call
699
- * @param byref array $params The parameters to send along
696
+ * @param string $method The API method to call
697
+ * @param array $params The parameters to send along
700
698
*
701
699
* @return void
702
700
*/
703
- protected function _detectFilenames ($ method , & $ params )
701
+ protected function _buildMultipart ($ method , $ params )
704
702
{
705
703
// well, files will only work in multipart methods
706
704
if (! $ this ->_detectMultipart ($ method )) {
@@ -721,42 +719,54 @@ protected function _detectFilenames($method, &$params)
721
719
return ;
722
720
}
723
721
724
- // check for filenames
725
722
$ possible_files = explode (' ' , $ possible_files [$ method ]);
726
- foreach ( $ possible_files as $ possible_file ) {
727
- // is this parameter set currently?
728
- if (! isset ( $ params [ $ possible_file ])) {
729
- continue ;
730
- }
723
+
724
+ $ multipart_border = ' -------------------- ' . $ this -> _nonce ();
725
+ $ multipart_request = '' ;
726
+
727
+ foreach ( $ params as $ key => $ value ) {
731
728
// is it an array?
732
- if (is_array ($ params [ $ possible_file ] )) {
729
+ if (is_array ($ value )) {
733
730
throw new \Exception ('Using URL-encoded parameters is not supported for uploading media. ' );
734
731
continue ;
735
732
}
736
- // is it a file, a readable one?
737
- if (! @file_exists ($ params [$ possible_file ])
738
- || ! @is_readable ($ params [$ possible_file ])
739
- ) {
740
- continue ;
741
- }
742
- // is it a valid image?
743
- if (! $ data = @getimagesize ($ params [$ possible_file ])) {
744
- continue ;
745
- }
746
- // is it a supported image format?
747
- if (! in_array ($ data [2 ], $ this ->_supported_media_files )) {
748
- continue ;
749
- }
750
- // try to read the file
751
- ob_start ();
752
- readfile ($ params [$ possible_file ]);
753
- $ data = ob_get_contents ();
754
- ob_end_clean ();
755
- if (strlen ($ data ) == 0 ) {
756
- continue ;
733
+ $ multipart_request .=
734
+ '-- ' . $ multipart_border . "\r\n"
735
+ . 'Content-Disposition: form-data; name=" ' . $ key . '" ' ;
736
+
737
+ // check for filenames
738
+ if (in_array ($ key , $ possible_files )) {
739
+ $ multipart_request .=
740
+ "\r\nContent-Transfer-Encoding: base64 " ;
741
+
742
+ if (// is it a file, a readable one?
743
+ @file_exists ($ value )
744
+ && @is_readable ($ value )
745
+
746
+ // is it a valid image?
747
+ && $ data = @getimagesize ($ params [$ possible_file ])
748
+
749
+ // is it a supported image format?
750
+ && in_array ($ data [2 ], $ this ->_supported_media_files )
751
+ ) {
752
+ // try to read the file
753
+ ob_start ();
754
+ readfile ($ value );
755
+ $ data = ob_get_contents ();
756
+ ob_end_clean ();
757
+ if (strlen ($ data ) == 0 ) {
758
+ continue ;
759
+ }
760
+ $ value = $ data ;
761
+ }
757
762
}
758
- $ params [$ possible_file ] = $ data ;
763
+
764
+ $ multipart_request .=
765
+ "\r\n\r\n" . $ value . "\r\n" ;
759
766
}
767
+ $ multipart_request .= '-- ' . $ multipart_border . '-- ' ;
768
+
769
+ return $ multipart_request ;
760
770
}
761
771
762
772
@@ -806,15 +816,16 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
806
816
$ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
807
817
$ ch = curl_init ($ url_with_params );
808
818
} else {
809
- $ authorization = $ this ->_sign ($ httpmethod , $ url , array ());
810
- if (! $ multipart ) {
819
+ if ($ multipart ) {
820
+ $ authorization = $ this ->_sign ($ httpmethod , $ url , array ());
821
+ $ params = $ this ->_buildMultipart ($ method_template , $ params );
822
+ } else {
811
823
$ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
812
824
$ params = http_build_query ($ params );
813
825
}
814
- $ post_fields = $ params ;
815
826
$ ch = curl_init ($ url );
816
827
curl_setopt ($ ch , CURLOPT_POST , 1 );
817
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ post_fields );
828
+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ params );
818
829
}
819
830
if ($ app_only_auth ) {
820
831
if (self ::$ _oauth_consumer_key == null ) {
@@ -826,17 +837,25 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
826
837
}
827
838
$ authorization = 'Authorization: Bearer ' . self ::$ _oauth_bearer_token ;
828
839
}
840
+ $ request_headers = array ();
841
+ if (isset ($ authorization )) {
842
+ $ request_headers [] = $ authorization ;
843
+ $ request_headers [] = 'Expect: ' ;
844
+ }
845
+ if ($ multipart ) {
846
+ $ first_newline = strpos ($ params , "\r\n" );
847
+ $ multipart_boundary = substr ($ params , 2 , $ first_newline );
848
+ $ request_headers [] = 'Content-Type: multipart/form-data; boundary= '
849
+ . $ multipart_boundary ;
850
+ }
851
+ print_r ($ request_headers );die ();
829
852
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
830
853
curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , 0 );
831
854
curl_setopt ($ ch , CURLOPT_HEADER , 1 );
832
855
curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , 0 );
833
856
curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
834
- if (isset ($ authorization )) {
835
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , array (
836
- $ authorization ,
837
- 'Expect: '
838
- ));
839
- }
857
+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ request_headers );
858
+
840
859
$ reply = curl_exec ($ ch );
841
860
$ httpstatus = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
842
861
$ reply = $ this ->_parseApiReply ($ method_template , $ reply );
0 commit comments