1

I am trying to get the file from a 3-d party server using \Magento\Framework\HTTP\Adapter\Curl in my model. My aim is to record this file in the file on my server inside magento. The thing is the request passes successfully, I am getting the reply, but the file is created blank (zero byte file) and nothing is recorded in it. Here is the example of my code:

/**
 * @var \Magento\Framework\Filesystem\Directory\Write
 * @var \Magento\Framework\HTTP\Adapter\Curl
 * @var string $destination
 * @var string $source
*/
$newFile = $this->_directory->getDriver()->fileOpen($destination, "wb");
$this->_curl->addOption(CURLOPT_FILE, $newFile);
$this->_curl->write(\Zend_Http_Client::GET, $source);
$result = $this->_curl->read();

As a result i have the full reply in $result , the blank file in $destination .There are enough permissions i.e. even setting 777 on the directory and a current file, the result is not changing.

drwxrwxrwx 3 501 www-data 102 Apr  5 08:56 .
drwxrwxrwx 3 501 www-data 102 Apr  5 08:10 ..
-rw-rw-rw- 1 501 www-data   0 Apr  5 08:59 file_temp.gz
2

Try to get the file with Magento\Framework\HTTP\ZendClient like this. ZendClientFactory will create instance of Magento\Framework\HTTP\ZendClient class:

/**
 * @var \Magento\Framework\HTTP\ZendClientFactory
 * @var string $url
 */
$client = $this->_httpClientFactory->create();
$client->setUri($url);
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
$result = $client->request(\Zend_Http_Client::GET)->getBody();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.