Skip to content

Commit

Permalink
Fixes the 'Empty reply fromm server' error (API Error bigcommerce#52)…
Browse files Browse the repository at this point in the history
… as well as another bug where too many files are opened by PHP.

The changes were made to the put function found in the `src/Bigcommerce/Api/Connection.php` and involves only the way the file handler for the temporary CURL file is used and managed by doing the following:
 - Checking for a valid instance of the `$this->handle`,
 - If no valid `$this->handle` was found, opens a new temp file with tmpfile() and assigns to `$this->handle`
 - Performs an additional check to see if the temp file contains any content from a previous use of the file, truncates the file to 0 bytes, closes and reopens the file, and rewinds the pointer to the beginning of the file
Then the put function proceeds as before by writing the body of the request to the file.
  • Loading branch information
Sebastian Forsberg committed Feb 7, 2014
1 parent cf034e3 commit b0cb4bd
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Bigcommerce/Api/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,28 @@ public function put($url, $body)

if (!is_string($body)) {
$body = json_encode($body);
}
}

$this->initializeRequest();

$handle = tmpfile();
fwrite($handle, $body);
fseek($handle, 0);
curl_setopt($this->curl, CURLOPT_INFILE, $handle);
$handle_type = ( isset( $this->handle ) ) ? get_resource_type( $this->handle ) : false;
if( $handle_type === false || strcmp( $handle_type, 'stream' ) !== 0 ){
$this->handle = tmpfile();
}

$current_content = fread( $this->handle, 8192 );
if( strlen( $current_content !== 0 ) ){
ftruncate( $this->handle, 0 );
fclose( $this->handle );

$this->handle = tmpfile();
rewind( $this->handle );
}

fwrite( $this->handle, $body );
rewind( $this->handle );

curl_setopt($this->curl, CURLOPT_INFILE, $this->handle);
curl_setopt($this->curl, CURLOPT_INFILESIZE, strlen($body));

curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
Expand Down

0 comments on commit b0cb4bd

Please sign in to comment.