Home > Software engineering >  How to use curl in Queue with Laravel
How to use curl in Queue with Laravel

Time:10-02

I am using queue for the first time in Laravel. I can't seem to get it work. I am sending an email and also calling a url with curl ().

I have even tried file_content_get(), yet it doesn't seem to work. The email seems to work just fine...

My question is: is there a different approach to calling an endpoint using Queue?

public function handle()
{
    $email = new Airtime();
    $ch = curl_init("some-url");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // exec($ch);
    curl_exec($ch);
    Mail::to($this->details['email'])->send($email);
}

The email gets sent, but the curl is completely ignored.

CodePudding user response:

Unless the function is disabled in php.ini in disable_functions directive or something blocks your requests on the network level, there is no specific reason this should not be executed. Are you sure that the endpoint being called did not in fact receive the request?

Calling remote endpoints from queues works fine for me. I just tested it now with a snippet below:

 /**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    $result = \Http::get('https://api.publicapis.org/entries')->json('entries');
    \Log::info($result);
}

Result:

    [2022-10-01 11:12:25] local.INFO: array (
  0 => 
  array (
    'API' => 'AdoptAPet',
    'Description' => 'Resource to help get pets adopted',
    'Auth' => 'apiKey',
    'HTTPS' => true,
    'Cors' => 'yes',
    'Link' => 'https://www.adoptapet.com/public/apis/pet_list.html',
    'Category' => 'Animals',
  ),
  1 => 
  array (
    'API' => 'Axolotl',
    'Description' => 'Collection of axolotl pictures and facts',
    'Auth' => '',
    'HTTPS' => true,
    'Cors' => 'no',
    'Link' => 'https://theaxolotlapi.netlify.app/',
    'Category' => 'Animals',
  ),
  2 => 
  array (
    'API' => 'Cat Facts',
    'Description' => 'Daily cat facts',
    'Auth' => '',
    'HTTPS' => true,
    'Cors' => 'no',
    'Link' => 'https://alexwohlbruck.github.io/cat-facts/',
    'Category' => 'Animals',
  ),
  3 => 
  array (
    'API' => 'Cataas',
    'Description' => 'Cat as a service (cats pictures and gifs)',
    'Auth' => '',
    'HTTPS' => true,
    'Cors' => 'no',
    'Link' => 'https://cataas.com/',
    'Category' => 'Animals',
  )
  • Related