Skip to content

Commit

Permalink
change request method to curl
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima K committed Mar 5, 2018
1 parent eb0a6df commit 9a50901
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Modules/Console/CompressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function actionIndex($path = null, $recursive = true)
$compressor = new CompressorApi();
foreach ($collection as $image) {
$image = str_replace($fronted, '', $image);
$image = str_replace('\\', '/', $image);
$image = trim($image, '\\');
$image = trim($image, '/');


$this->compressImage($image, $compressor);
}
Expand Down
30 changes: 16 additions & 14 deletions Services/RequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,27 @@ public function __construct()

public function sendRequest(string $imageExt, string $imageContent, int $compressRatio): string
{
$method = '/image';
$requestData = $this->prepareDataForRequest($imageExt, $imageContent, $compressRatio);
$target_url = $this->domain . '/image';

$options = [
'http' => [
'method' => 'POST',
'content' => http_build_query($requestData),
'ignore_errors' => true,
'header' => "Content-Type: application/x-www-form-urlencoded"
]
];
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $target_url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $this->prepareDataForRequest($imageExt, $imageContent, $compressRatio)
));
$request = curl_exec($ch);

$context = stream_context_create($options);
$request = file_get_contents($this->domain . $method, false, $context);
if (curl_errno($ch)) {
$message = curl_error($ch);
curl_close($ch);

if (!$this->checkResponseStatus($http_response_header)) {
throw new Exception($request);
throw new Exception($message);
}

curl_close($ch);
return $request;
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"minimum-stability": "stable",
"version": "1.0.2",
"version": "1.0.3",
"require": {
"yiisoft/yii2": "*",
"php": ">=7.0"
Expand Down

0 comments on commit 9a50901

Please sign in to comment.