8000 Build multipart body manually instead of relying on cURL · codezninja/codebird-php@62b6596 · GitHub
[go: up one dir, main page]

Skip to content

Commit 62b6596

Browse files
committed
Build multipart body manually instead of relying on cURL
1 parent 9933cdb commit 62b6596

File tree

1 file changed

+65
-46
lines changed

1 file changed

+65
-46
lines changed

src/codebird.php

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,6 @@ public function __call($fn, $params)
260260
$httpmethod = $this->_detectMethod($method_template, $apiparams);
261261
$multipart = $this->_detectMultipart($method_template);
262262

263-
// geek-geek: Now allowing to specify filenames as params
264-
$this->_detectFilenames($method_template, $apiparams);
265-
266263
return $this->_callApi(
267264
$httpmethod,
268265
$method,
@@ -693,14 +690,15 @@ protected function _detectMultipart($method)
693690
}
694691

695692
/**
696-
* Detects filenames in upload parameters
693+
* Detect filenames in upload parameters,
694+
* build multipart request from upload params
697695
*
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
700698
*
701699
* @return void
702700
*/
703-
protected function _detectFilenames($method, &$params)
701+
protected function _buildMultipart($method, $params)
704702
{
705703
// well, files will only work in multipart methods
706704
if (! $this->_detectMultipart($method)) {
@@ -721,42 +719,54 @@ protected function _detectFilenames($method, &$params)
721719
return;
722720
}
723721

724-
// check for filenames
725722
$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) {
731728
// is it an array?
732-
if (is_array($params[$possible_file])) {
729+
if (is_array($value)) {
733730
throw new \Exception('Using URL-encoded parameters is not supported for uploading media.');
734731
continue;
735732
}
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+
}
757762
}
758-
$params[$possible_file] = $data;
763+
764+
$multipart_request .=
765+
"\r\n\r\n" . $value . "\r\n";
759766
}
767+
$multipart_request .= '--' . $multipart_border . '--';
768+
769+
return $multipart_request;
760770
}
761771

762772

@@ -806,15 +816,16 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
806816
$authorization = $this->_sign($httpmethod, $url, $params);
807817
$ch = curl_init($url_with_params);
808818
} 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 {
811823
$authorization = $this->_sign($httpmethod, $url, $params);
812824
$params = http_build_query($params);
813825
}
814-
$post_fields = $params;
815826
$ch = curl_init($url);
816827
curl_setopt($ch, CURLOPT_POST, 1);
817-
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
828+
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
818829
}
819830
if ($app_only_auth) {
820831
if (self::$_oauth_consumer_key == null) {
@@ -826,17 +837,25 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
826837
}
827838
$authorization = 'Authorization: Bearer ' . self::$_oauth_bearer_token;
828839
}
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();
829852
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
830853
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
831854
curl_setopt($ch, CURLOPT_HEADER, 1);
832855
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
833856
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+
840859
$reply = curl_exec($ch);
841860
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
842861
$reply = $this->_parseApiReply($method_template, $reply);

0 commit comments

Comments
 (0)
0