-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Prokl\InstagramParserRapidApiBundle\Services\Transport; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* Class CurlDownloader | ||
* @package Prokl\InstagramParserRapidApiBundle\Services\Transport | ||
* | ||
* @since 21.05.2021 | ||
*/ | ||
class CurlDownloader | ||
{ | ||
/** | ||
* @var string $documentRoot DOCUMENT_ROOT. | ||
*/ | ||
private $documentRoot; | ||
|
||
/** | ||
* CurlDownloader constructor. | ||
* | ||
* @param string $documentRoot DOCUMENT_ROOT. | ||
*/ | ||
public function __construct(string $documentRoot) | ||
{ | ||
$this->documentRoot = $documentRoot; | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @param string $dest | ||
* | ||
* @return string | ||
* @throws RuntimeException Когда проблемы с закачкой файла. | ||
* | ||
* @internal Если файл существует, то не перезаписывается. | ||
*/ | ||
public function download(string $url, string $dest) : string | ||
{ | ||
if (@file_exists($this->documentRoot . $dest)) { | ||
return $dest; | ||
} | ||
|
||
$curl = curl_init(); | ||
|
||
curl_setopt_array( | ||
$curl, | ||
[ | ||
CURLOPT_URL => $url, | ||
CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_FOLLOWLOCATION => true, | ||
CURLOPT_ENCODING => '', | ||
CURLOPT_MAXREDIRS => 10, | ||
CURLOPT_TIMEOUT => 30, | ||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | ||
CURLOPT_CUSTOMREQUEST => 'GET', | ||
CURLOPT_SSL_VERIFYHOST => false, | ||
CURLOPT_SSL_VERIFYPEER => false, | ||
]); | ||
|
||
$response = curl_exec($curl); | ||
$err = curl_error($curl); | ||
|
||
curl_close($curl); | ||
|
||
if ($err) { | ||
throw new RuntimeException('Get Request Error: ' . $err); | ||
} | ||
|
||
$fp = fopen($this->documentRoot . $dest, 'w'); | ||
|
||
if ($fp === false) { | ||
throw new RuntimeException('File error: ' . $this->documentRoot . $dest); | ||
} | ||
|
||
$success = fwrite($fp, $response); | ||
if ($success === false) { | ||
throw new RuntimeException('File write error: ' . $this->documentRoot . $dest); | ||
} | ||
|
||
fclose($fp); | ||
|
||
return $dest; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters