在 laravel 11 中我们使用了 use GuzzleHttp\Client;使用 GuzzleHttp\Promise;

问题描述 投票:0回答:1

使用 GuzzleHttp\Client; 使用 GuzzleHttp\Promise;

公共函数all_data() {

$drivers = DB::table('drivers')->orderBy('id', 'desc')->get();
$client = new Client();


$promises = [];

foreach ($drivers as $driver) {
    $dateofbirth = $driver->date_of_birth;
    $dobtimestamp = strtotime($dateofbirth);
    $dob = $dobtimestamp * 1000;

    $url = 'https://testing/api';
    $token = '8bykxzrBJbhxFMZCrDNYH8aeZT/W4=';
    $username = '15291';

    $payload = [
        "serviceName" => "SelectorService",
        "serviceMethod" => "dateBankSelector",
        "user" => [
            "mobileNo" => $driver->mobile,
            "alternateMobileNo" => "9642433789",
            "emailId" => $driver->email,
            "firstName" => $driver->first_name,
            "lastName" => $driver->last_name,
            "state" => $driver->state,
            "dob" => $dob,
            "gender" => ucfirst($driver->gender)
            
        ],
        "external_entity_name" => ["externalEntityNameId" => 17],
        "external_entity_type" => ["externalEntityTypeId" => 17],
        
        "polName" => $driver->price,
        "RefNumber" => $this->generateUniqueId(),
        "productDuration" => "9",
        "policyRenewalDate" => "1649050978983",
        "executiveMobileNo" => "4545452299",
        "executiveEmailId" => "[email protected]",
        "referenceField1" => "Hello Team",
        "referenceField2" => "We are test all the mobile applications"
        
    ];

   
    $promises[$driver->id] = $client->postAsync($url, [
        'json' => $payload,
        'headers' => [
            'Content-Type' => 'application/json',
            'authentication-token' => $token,
            'authentication-username' => $username
        ]
    ]);
}


Promise::each($promises, function ($response, $driverId) {
    if ($response->getStatusCode() === 200) {
        $result = $response->getBody();
        $data = json_decode($result, true);

        if (isset($data['redirectionLink'])) {
            $paymentLink = $data['redirectionLink'];

            DB::table('drivers')
                ->where('id', $driverId)
                ->update(['redirectionLink' => $paymentLink]);

            
            $updatePayload = [
                "mobileNumber" => DB::table('drivers')->where('id', $driverId)->value('mobile'),
                "PaymentLink" => $paymentLink
            ];

            $updateResponse = Http::withHeaders([
                'Content-Type' => 'application/json',
                'Authorization' => env('LEADS_API_TOKEN')
            ])->post('https://24x7/rest/updateFormData', $updatePayload);

            if (!$updateResponse->successful()) {
                Log::warning('Failed to update payment link for driver ID ' . $driverId);
            }
        }
    } else {
        Log::warning('Request failed for driver ID ' . $driverId);
    }
})->wait();

return view('backend.all_drivers', ['drivers' => $drivers]);

}

执行此函数时出现此错误

未找到“GuzzleHttp\Promise”类

当执行此函数时,会生成该驱动程序的支付链接,并应立即更新,早些时候此函数加载时间过多并抛出 504 超时错误,这就是为什么我使用 Guzzle/http 进行快速响应和重新加载,因为我们有大量记录。

php laravel guzzle
1个回答
0
投票
  1. 安装缺少的软件包 确保您已安装 Guzzle HTTP 库,其中包括 guzzlehttp/promises。

在 Laravel 项目的根目录中运行以下命令:

composer require guzzlehttp/guzzle
  1. 验证您在代码中为 Promise 类使用了正确的命名空间。例如:

    使用 GuzzleHttp\Promise\Promise;

© www.soinside.com 2019 - 2024. All rights reserved.