8000 Support HTTP proxy replies · devildeveloper/codebird-php@422aa88 · GitHub
[go: up one dir, main page]

Skip to content

Commit 422aa88

Browse files
committed
Support HTTP proxy replies
1 parent 62b6596 commit 422aa88

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ codebird-php - changelog
33

44
2.4.0 (not yet released)
55
+ rfe #21 JSON return format
6+
+ Support HTTP proxy replies
67

78
2.3.7 (not yet released)
89
+ Add contributing guidelines

src/codebird.php

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -736,29 +736,31 @@ protected function _buildMultipart($method, $params)
736736

737737
// check for filenames
738738
if (in_array($key, $possible_files)) {
739-
$multipart_request .=
740-
"\r\nContent-Transfer-Encoding: base64";
741-
742739
if (// is it a file, a readable one?
743740
@file_exists($value)
744741
&& @is_readable($value)
745742

746743
// 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)
744+
&& $data = @getimagesize($value)
751745
) {
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;
746+
if (// is it a supported image format?
747+
in_array($data[2], $this->_supported_media_files)
748+
) {
749+
// try to read the file
750+
ob_start();
751+
readfile($value);
752+
$data = ob_get_contents();
753+
ob_end_clean();
754+
if (strlen($data) == 0) {
755+
continue;
756+
}
757+
$value = $data;
759758
}
760-
$value = $data;
761759
}
760+
761+
$multipart_request .=
762+
"\r\nContent-Transfer-Encoding: base64";
763+
$value = base64_encode($value);
762764
}
763765

764766
$multipart_request .=
@@ -823,7 +825,7 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
823825
$authorization = $this->_sign($httpmethod, $url, $params);
824826
$params = http_build_query($params);
825827
}
826-
$ch = curl_init($url);
828+
$ch = curl_init($url);
827829
curl_setopt($ch, CURLOPT_POST, 1);
828830
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
829831
}
@@ -845,15 +847,16 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
845847
if ($multipart) {
846848
$first_newline = strpos($params, "\r\n");
847849
$multipart_boundary = substr($params, 2, $first_newline);
848-
$request_headers[] = 'Content-Type: multipart/form-data; boundary='
850+
$request_headers[] = 'Content-Type: multipart/form-data; boundary='
849851
. $multipart_boundary;
850852
}
851-
print_r($request_headers);die();
853+
852854
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
853855
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
854856
curl_setopt($ch, CURLOPT_HEADER, 1);
855-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
856-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
857+
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
858+
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
859+
// curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
857860
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
858861

859862
$reply = curl_exec($ch);
@@ -879,7 +882,19 @@ protected function _parseApiReply($method, $reply)
879882
{
880883
// split headers and body
881884
$headers = array();
882-
$reply = explode("\r\n\r\n", $reply, 2);
885+
$reply = explode("\r\n\r\n", $reply, 4);
886+
887+
// check if using proxy
888+
if (substr($reply[0], 0, 35) === 'HTTP/1.1 200 Connection Established') {
889+
array_shift($reply);
890+
} elseif (count($reply) > 2) {
891+
$headers = array_shift($reply);
892+
$reply = array(
893+
$headers,
894+
implode("\r\n", $reply)
895+
);
896+
}
897+
883898
$headers_array = explode("\r\n", $reply[0]);
884899
foreach ($headers_array as $header) {
885900
$header_array = explode(': ', $header, 2);

0 commit comments

Comments
 (0)
0