<p>Make your request through the <a href="https://craftcms.com/docs/3.x/extend/queue-jobs.html#adding-your-job-to-the-queue" rel="nofollow noreferrer">job</a>. During the event, if the conditions matched, just push the job to the queue.</p>
<p>For example, if you are collecting statistics or something like that, it will not block the thread and the user will not have to wait until the API request is executed.</p>
<p>BTW, why don't you use Guzzly for API call?</p>

CC BY-SA 4.0
2022-12-08T22:13:01.950Z
2022-12-08T22:19:06.883Z
2022-12-08T22:19:06.883Z
RomanAvr
<p>I would like to send a post request to an API when a new order in in Craft Commerce is paid.
So I've got this event into a plugin:</p>
<pre><code>Event::on(
            Order::class,
            OrderStatuses::class,
            Order::EVENT_AFTER_ORDER_AUTHORIZED,
                function(Event $event) {
                $order = $event-&gt;sender;
                $shippingAddress = $order-&gt;getShippingAddress();
                $reference = $order-&gt;reference();
                
                $mail_array = array();
                $mail_array[] = array('email' =&gt; $order-&gt;email, 'country' =&gt; $shippingAddress-&gt;getCountry()-&gt;iso, 'reference' =&gt; $reference);
                
                // Se ci sono ordini effetivamente cambiati chiamo servizio
                if(count($mail_array) &gt; 0){
                    // API URL
                    $url = 'MY URI';
                    // Create a new cURL resource
                    $ch = curl_init($url);
                    // Setup request to send json via POST
                    $payload = json_encode($mail_array);
                    // Attach encoded JSON string to the POST fields
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
                    // Set the content type to application/json
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
                    // Return response instead of outputting
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    // Execute the POST request
                    $result = curl_exec($ch);
                    // Close cURL resource
                    curl_close($ch);
                } 
                return $order;
                
            } 
        );
</code></pre>
<p>I noticed the order in correctly created but the curl call &quot;blocks the flow&quot;.
If I try something easy like that it works:</p>
<pre><code>        Event::on(
            Order::class,
            OrderStatuses::class,
            Order::EVENT_AFTER_ORDER_AUTHORIZED,
                function(Event $event) {
                $order = $event-&gt;sender;
                $shippingAddress = $order-&gt;getShippingAddress();
                $reference = $order-&gt;reference();
                
                $mail_array = array();
                $mail_array[] = array('email' =&gt; $order-&gt;email, 'country' =&gt; $shippingAddress-&gt;getCountry()-&gt;iso, 'reference' =&gt; $reference);
                
 
                $order-&gt;status = 2;
                return $order;
                
            } 
        );
</code></pre>
<p>Then I would like to know if someone has experience with events and send e API request or if you have a doc or something can help me.</p>
<p>Thanks in advance and kind regards,</p>
<p>Davide</p>

CC BY-SA 4.0
2022-12-09T08:52:45.973Z
Hi @RomanAvr I'll check! Now I convert the call with Guzzly.
I didn't use it because I know it now :D Thanks and kind regards, Davide
Davide
<events><ecommerce><api>
Call external API when event is firing
# Call external API when event is firing

(Asked by: username_0 on 2022-12-08)

I would like to send a post request to an API when a new order in in Craft Commerce is paid.
So I've got this event into a plugin:
Event::on(
            Order::class,
            OrderStatuses::class,
            Order::EVENT_AFTER_ORDER_AUTHORIZED,
                function(Event $event) {
                $order = $event->sender;
                $shippingAddress = $order->getShippingAddress();
                $reference = $order->reference();
                
                $mail_array = array();
                $mail_array[] = array('email' => $order->email, 'country' => $shippingAddress->getCountry()->iso, 'reference' => $reference);
                
                // Se ci sono ordini effetivamente cambiati chiamo servizio
                if(count($mail_array) > 0){
                    // API URL
                    $url = 'MY URI';
                    // Create a new cURL resource
                    $ch = curl_init($url);
                    // Setup request to send json via POST
                    $payload = json_encode($mail_array);
                    // Attach encoded JSON string to the POST fields
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
                    // Set the content type to application/json
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
                    // Return response instead of outputting
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    // Execute the POST request
                    $result = curl_exec($ch);
                    // Close cURL resource
                    curl_close($ch);
                } 
                return $order;
                
            } 
        );

I noticed the order in correctly created but the curl call "blocks the flow".
If I try something easy like that it works:
        Event::on(
            Order::class,
            OrderStatuses::class,
            Order::EVENT_AFTER_ORDER_AUTHORIZED,
                function(Event $event) {
                $order = $event->sender;
                $shippingAddress = $order->getShippingAddress();
                $reference = $order->reference();
                
                $mail_array = array();
                $mail_array[] = array('email' => $order->email, 'country' => $shippingAddress->getCountry()->iso, 'reference' => $reference);
                
 
                $order->status = 2;
                return $order;
                
            } 
        );

Then I would like to know if someone has experience with events and send e API request or if you have a doc or something can help me.
Thanks in advance and kind regards,
Davide

**Tags:** events, ecommerce, api
> **Comments:**
> **username_0**: Hi @RomanAvr I'll check! Now I convert the call with Guzzly. I didn't use it because I know it now :D Thanks and kind regards, Davide  

---

## 1 answers

### Answer by username_1 on 2022-12-08. Score: 1 | Accepted answer

Make your request through the [job](https://craftcms.com/docs/3.x/extend/queue-jobs.html#adding-your-job-to-the-queue). During the event, if the conditions matched, just push the job to the queue.

For example, if you are collecting statistics or something like that, it will not block the thread and the user will not have to wait until the API request is executed.

BTW, why don't you use Guzzly for API call?


---

