@@ -17,11 +17,13 @@ public function __construct($debug = false)
1717 {
1818 $ this ->debug = $ debug ;
1919 }
20+
2021 /**
2122 *
2223 * @param $localPath string local file path to save
2324 */
24- public function setOption ($ key ,$ value ) {
25+ public function setOption ($ key , $ value )
26+ {
2527 $ this ->options [$ key ] = $ value ;
2628 }
2729
@@ -194,13 +196,36 @@ private function _exec($options, $returnInfo = false)
194196 }
195197 if (isset ($ this ->options ['local_file_handler ' ])) {
196198 $ fp = $ this ->options ['local_file_handler ' ];
197- flock ($ fp ,LOCK_EX );
199+ flock ($ fp , LOCK_EX );
198200 $ options [CURLOPT_WRITEFUNCTION ] = function ($ ch , $ string ) use ($ fp ) {
199201 $ length = fwrite ($ fp , $ string );
202+
200203 return $ length ;
201204 };
202205 fflush ($ fp );
203206 }
207+ // auto add content-length header
208+ $ has_content_length_header = false ;
209+ foreach ($ options [CURLOPT_HTTPHEADER ] as $ header ) {
210+ if (stripos ($ header , 'content-length: ' ) === 0 ) {
211+ $ has_content_length_header = true ;
212+ break ;
213+ }
214+ }
215+ if (!$ has_content_length_header && !isset ($ options [CURLOPT_INFILE ]) && !isset ($ options [CURLOPT_INFILESIZE ])) {
216+ if (isset ($ options [CURLOPT_POSTFIELDS ])) {
217+ // only for string content
218+ if (is_string ($ options [CURLOPT_POSTFIELDS ]) && strpos ($ options [CURLOPT_POSTFIELDS ], '@ ' ) !== 0 ) {
219+ $ options [CURLOPT_HTTPHEADER ] = array_merge (
220+ $ options [CURLOPT_HTTPHEADER ],
221+ ['Content-Length: ' .strlen ($ options [CURLOPT_POSTFIELDS ])]
222+ );
223+ }
224+ } else {
225+ $ options [CURLOPT_HTTPHEADER ] = array_merge ($ options [CURLOPT_HTTPHEADER ], ['Content-Length: 0 ' ]);
226+ }
227+ }
228+
204229 curl_setopt_array ($ ch , $ options );
205230 // force clean memory before getting more data
206231 $ this ->cleanLastRequest ();
0 commit comments