Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ap3twe's avatar

Returning Response from API To insert into database 1 request

Is there a way to get a response from API request and insert it to the database asynchronous?

public function store(Doctors $doctors, Request $request) {
$doctors = Doctors::create(array_merge(request([
            'doctor_email',
            'doctor_telephone',
            'totalCost',
            'case_number'
            ]),
            ['user_id' => auth()->id(),
             'totalCost' => $GLOBALS['totalCost'],
            // 'case_number' => $GLOBALS['caseNumber'], Fix this problem
            ]));

//  This is where I send the request to Api
           $this->AddCaseWebPortal ($request, $doctorsChrome);

}

// The API request

public function AddCaseWebPortal (Request $request, Doctors $doctors){
     
$clienttest2 = new SoapClient('***');
$paramstest2 = array(
         'addCaseReq' => array(
             'Case' => array
                 'ShipDate' => date("Y-m-d"),
                 'DateIn'=> date("Y-m-d"),
                 'Address'=> $address,
                 'CustomerID' => $customerId,
                 'PatientFirst' => $request->patient_firstname,
try {
       $responsetest2 = $clienttest2->__soapCall("AddCase", array($paramstest2));
     } catch (Exception $e) {
         echo 'Caught exception: ',  $e->getMessage(), "\r\n";
     }
 }
     // I get the response 

     //    dd($responsetest2->AddCaseResult->CaseNumber);
     //    $GLOBALS['caseNumber'] = $responsetest2->AddCaseResult->CaseNumber;
0 likes
6 replies
Tray2's avatar

You could use ajax as l to load from the external api and then insert it but then you risk those interuptions.

I would most likely use a queue worker for that. That way it will be done in the background instead of risking interuptions by the user.

https://laravel.com/docs/8.x/queues

1 like
Ap3twe's avatar

If I use Jobs, Will the queued job wait till it accepts the $Value from the response? I know I will pass the $variable to the job. but am not sure whether by the time the Request makes the API request and receives the response, the queue job hasn't fired. Hope you get what I mean?

Snapey's avatar

what do you mean? run multiple soap calls in parallel?

create jobs for each request then setup multiple queue workers?

Ap3twe's avatar

I sending a request to two places, DB and SOAP API

  1. I get a request form from a user where I store it in DB
  2. I send the request to the Soap Call parallel
  3. The soap call I receive a response Case ID I want to insert into the DB with the DB request
Snapey's avatar

insert the record into the db and note the created record id.

create a queued job which contains the created record id

send the data to the api via the queued job

When you get the response, store in the same record identified in the first step and resave.

Please or to participate in this conversation.