0

I'm new to magento module development and trying to write my own little module. Unfortunately, I'm having trouble understanding how to connect events to a REST request.

What I want to achieve:

  • Whenever a customer buys a product, a REST request should be executed.
  • The SKU of the product should be supplied to the rest request.
$post = [
    'sku' => $productSku
];

$ch = curl_init('https://api.example.com/path');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

$response = curl_exec($ch);
curl_close($ch);

I am using the latest version of Magento 2 CE.

Thank you for your time.

Best Regards, Piotr

2 Answers 2

1

Here's a quick run down of how you could achieve that:

https://devdocs.magento.com/guides/v2.4/extension-dev-guide/events-and-observers.html

  1. add a event listener in the global scope. the event should probably be "checkout_submit_all_after"
  2. In your PHP Observer you should use the class \Magento\Framework\HTTP\Adapter\Curl to execute the rest call
1
  • Thank you Philipp. Will try it out.
    – Piotr
    Jun 10, 2022 at 10:49
1

To add to Philipp Sander answare, this doc is really good for the consumption of external API.

https://devdocs.magento.com/guides/v2.4/ext-best-practices/tutorials/create-integration-with-api.html

1
  • Thanks a lot for your additional comment.
    – Piotr
    Jun 10, 2022 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.