0

How do I make a post request with curl without using a button, but instead calling the external API when a field is filled out

3 Answers 3

0

If you are working within Magento Try to use magento logic where possible rather than native php functions

/**
 * @var \Magento\Framework\HTTP\Client\Curl
 */
protected $_curl;

/**
 * @param Context                             $context
 * @param \Magento\Framework\HTTP\Client\Curl $curl
 */
public function __construct(
    Context $context,
    \Magento\Framework\HTTP\Client\Curl $curl
) {
    $this->_curl = $curl;
    parent::__construct($context);
}

public function execute()
{
    //if the method is get
    $this->_curl->get($url);
    //if the method is post
    $this->_curl->post($url, $params);
    //response will contain the output in form of JSON string
    $response = $this->_curl->getBody();
}

More can be found here

https://github.com/magento/magento2/blob/2.3/lib/internal/Magento/Framework/HTTP/Client/Curl.php

0
  1. Determine the API endpoint: Identify the URL of the API endpoint that you want to call.

  2. Initialize cURL: In your Magento 2 code, you can initialize cURL by creating a new cURL handle with curl_init(). For example:

     $curl = curl_init();
    
  3. Set the cURL options: Set the cURL options that you need for your API call. For example, you may need to set the URL, the request method, and any headers or parameters. Here's an example:

         curl_setopt_array($curl, array(
             CURLOPT_URL => "https://api.example.com/endpoint",
             CURLOPT_RETURNTRANSFER => true,
             CURLOPT_ENCODING => "",
             CURLOPT_MAXREDIRS => 10,
             CURLOPT_TIMEOUT => 30,
             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
             CURLOPT_CUSTOMREQUEST => "POST",
             CURLOPT_POSTFIELDS => "param1=value1&param2=value2",
             CURLOPT_HTTPHEADER => array(
                 "Content-Type: application/x-www-form-urlencoded",
                 "Authorization: Bearer my_token"
             ),
         ));
    

In this example, the cURL options include the API endpoint URL, the request method (POST), the request parameters, and the Authorization header with a bearer token.

  1. Execute the cURL request: Use the curl_exec() function to execute the cURL request. For example:

     $response = curl_exec($curl);
    

This will execute the cURL request and store the response in the $response variable.

  1. Handle the response: You can then handle the response from the API as needed. For example, you may need to parse the JSON data in the response or check for errors.

  2. Close the cURL handle: Finally, close the cURL handle with curl_close(). For example:

     curl_close($curl);
    
-2

You need to create a custom controller and post data in that through ajax, then use curl in the controller:

  $curl_handle=curl_init();
  curl_setopt($curl_handle,CURLOPT_URL,'http://www.google.com');
  curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
  curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  $buffer = curl_exec($curl_handle);
  curl_close($curl_handle);
  if (empty($buffer)){
      print "Nothing returned from url.<p>";
  }
  else{
      print $buffer;
  }

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.