使用 guzzle http 替代 curl

This commit is contained in:
iVampireSP.com 2023-07-01 12:32:49 +08:00
parent 41c3345a84
commit 754607ca7a
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132

View File

@ -2,6 +2,7 @@
namespace App\Support; namespace App\Support;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class WHMCS class WHMCS
@ -67,19 +68,11 @@ private function request($action, $params = []): ?array
], $params); ], $params);
$ch = curl_init(); $http = Http::asForm()->post($this->url . '/includes/api.php', $params)->throw();
curl_setopt($ch, CURLOPT_URL, $this->url . '/includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1); $response = $http->body();
curl_setopt( $json = $http->json();
$ch,
CURLOPT_POSTFIELDS,
http_build_query($params)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response, true);
if (is_null($json)) { if (is_null($json)) {
Log::error('WHMCS response is not valid JSON', [$response]); Log::error('WHMCS response is not valid JSON', [$response]);
@ -104,19 +97,11 @@ public function api($action, $params = []): ?array
$url = $this->url . '/modules/addons/PortIOInvoice/api/' . $action . '.php'; $url = $this->url . '/modules/addons/PortIOInvoice/api/' . $action . '.php';
$ch = curl_init(); $http = Http::asForm()->post($url, $params)->throw();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1); $code = $http->status();
curl_setopt( $response = $http->body();
$ch, $json = $http->json();
CURLOPT_POSTFIELDS,
http_build_query($params)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$json = json_decode($response, true);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (is_null($json)) { if (is_null($json)) {
Log::error('WHMCS response is not valid JSON', [$url, $code, $response]); Log::error('WHMCS response is not valid JSON', [$url, $code, $response]);