最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - In laravel 11 we have used use GuzzleHttpClient; use GuzzleHttpPromise; - Stack Overflow

programmeradmin2浏览0评论
use GuzzleHttp\Client;
use GuzzleHttp\Promise;

public function 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]);
}

This error is occur when this function is execute

Class "GuzzleHttp\Promise" not found

When this function is execute then payment link generate of that driver and should be instantly updated , Earlier This function is load too much time adn throw 504 timeout error that's why I have used Guzzle/http for fast response and reload because we have bulk of records.

use GuzzleHttp\Client;
use GuzzleHttp\Promise;

public function 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]);
}

This error is occur when this function is execute

Class "GuzzleHttp\Promise" not found

When this function is execute then payment link generate of that driver and should be instantly updated , Earlier This function is load too much time adn throw 504 timeout error that's why I have used Guzzle/http for fast response and reload because we have bulk of records.

Share Improve this question edited Nov 21, 2024 at 16:12 Dharman 33.4k27 gold badges101 silver badges147 bronze badges Recognized by PHP Collective asked Nov 21, 2024 at 7:11 MukeshMukesh 1 0
Add a comment  | 

1 Answer 1

Reset to default 0
  1. Install the Missing Package Ensure you have installed the Guzzle HTTP library, which includes guzzlehttp/promises.

Run the following command in your Laravel project's root directory:

composer require guzzlehttp/guzzle
  1. Verify that you're using the correct namespace for the Promise class in your code. For example:

    use GuzzleHttp\Promise\Promise;

发布评论

评论列表(0)

  1. 暂无评论