diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 223d69e..c10c5fb 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -73,6 +73,26 @@ class Controller extends BaseController return $formatted !== '' ? $formatted : '0'; } + /** + * گزینه‌های Guzzle/cURL — فقط وقتی افزونه curl و ثابت‌ها موجود باشند. + * + * @param array $curlOptions + * @return array> + */ + public static function httpClientOptions(array $curlOptions = []): array + { + $curl = $curlOptions; + + if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) { + $resolveOpt = (int) constant('CURLOPT_IPRESOLVE'); + if (! array_key_exists($resolveOpt, $curl)) { + $curl[$resolveOpt] = (int) constant('CURL_IPRESOLVE_V4'); + } + } + + return $curl === [] ? [] : ['curl' => $curl]; + } + public static function Send($to , $msg, $is_customer = true) { $r = self::SendMessage($to , $msg, $is_customer); @@ -139,26 +159,32 @@ class Controller extends BaseController ]); $platforms[] = "0098:{$to}";*/ - $response = Http::timeout(15) - ->withHeaders([ - 'Content-Type' => 'application/json', - 'secret_key' => 'f21debfb-83ac-4624-a2f4-f08375043181', - ]) - ->withOptions([ - 'curl' => [ - \CURLOPT_IPRESOLVE => \CURL_IPRESOLVE_V4, - ] - ]) - ->post('http://185.231.112.101/api/createsms', [ + try { + $http = Http::timeout(15) + ->withHeaders([ + 'Content-Type' => 'application/json', + 'secret_key' => 'f21debfb-83ac-4624-a2f4-f08375043181', + ]); + + $curlOptions = self::httpClientOptions(); + if ($curlOptions !== []) { + $http = $http->withOptions($curlOptions); + } + + $response = $http->post('http://185.231.112.101/api/createsms', [ 'phone' => $to, 'host' => $config->Config('name_fa') ?? gethostname(), 'text' => trim($msg), 'platform' => 'rayegansms', ]); - $platforms[] = $response->successful() - ? "rayegansms:{$to}" - : "rayegansms_failed:{$to}:{$response->status()}"; + $platforms[] = $response->successful() + ? "rayegansms:{$to}" + : "rayegansms_failed:{$to}:{$response->status()}"; + } catch (\Throwable $e) { + report($e); + $platforms[] = "rayegansms_error:{$to}"; + } } diff --git a/app/Services/MontzarService.php b/app/Services/MontzarService.php index 10b729d..0b0a7eb 100644 --- a/app/Services/MontzarService.php +++ b/app/Services/MontzarService.php @@ -78,12 +78,9 @@ class MontzarService $response = Http::timeout(7) ->acceptJson() - ->withOptions([ - 'curl' => [ - CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, - CURLOPT_TCP_FASTOPEN => 1, - ], - ]) + ->withOptions(self::httpClientOptions( + defined('CURLOPT_TCP_FASTOPEN') ? [(int) constant('CURLOPT_TCP_FASTOPEN') => 1] : [] + )) ->withToken($token) ->get($this->endpoint('/server/customer/product/list')) ->json(); diff --git a/app/Services/WebsocketBroadcastService.php b/app/Services/WebsocketBroadcastService.php index 7b35c6d..6a116cb 100644 --- a/app/Services/WebsocketBroadcastService.php +++ b/app/Services/WebsocketBroadcastService.php @@ -84,11 +84,7 @@ class WebsocketBroadcastService try { $response = Http::timeout(3) ->withHeaders(['Content-Type' => 'application/json']) - ->withOptions([ - 'curl' => [ - CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, - ], - ]) + ->withOptions(\App\Http\Controllers\Controller::httpClientOptions()) ->post($url, $body); if (! $response->successful()) {