[Drupal] How to POST data to an external server using CURL?

By varokky.jose | 30th June 2015 | 1 min read

Many a times while developing sites we would need to post data to an external server, for example if you have an ecommerce site and wants to manage CRM in a different server at such time you would need to pass the order details to different server. We can POST the data using CURL or drupal_http_request().

We had a similar requirement where the order details has to be posted to an external server. We posted the data to the external server using CURL script. Following the code snippet which we used for posting the data.

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POST,TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

$url is the location where the data should be posted and $xml is the XML data to be poste.

Hope this helps.