user2997418
user2997418

Reputation: 658

Post data to external URL using cURL

I want to use cURL to post some data to an external URL, here is the code they ask to embed, for some technical needs I need to create my own form action so I need cURL so POST those data: This is what I am asked to embed:

<form action='https://external.com/blabla' name=715000002374001 method='POST'>  
<input type='text'name='xnQsjsdp' value='c942f375287f707b73'/>  
<input type='hidden' name='zc_gad' id='zc_gad' value=''/> 
 <input type='text'name='xmIwtLD' value=1aff1efd0a4'/>  
<input type='text' name='actionType' value='TGVhZHM='/> 
<input type='text' name='returnURL' value='http&#x3a;&#x2f;&#x2f;www.blabla.de' />
</form>

I try to make it with cURL :

  $data = 'xnQsjsdp=c942f375287f707b73&xmIwtLD=TGVhZHM&actionType=TGVhZHM&returnURL&http&#x3a;&#x2f;&#x2f;www.blabla.de';
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,'https://external.com/blabla');
  curl_setopt($ch, CURLOPT_URL,$url);
  //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data );
  $res = curl_exec ($ch);
  curl_close ($ch);

But seems not to be working !!!

Upvotes: 0

Views: 1492

Answers (1)

Mizanur Rahman Khan
Mizanur Rahman Khan

Reputation: 1861

You can use this class like

class YourClass
{

   private static $base_url = 'http://localhost/projects/';
   private static $key = 'your_key';
   private static $secret = 'your_secret';

   public static function yourMethodName($id = '', $fields = '', $is_return_transfer = 0)
   {

      if (is_array($fields))
      {
         $fields_str = http_build_query($fields, '', '&');
      } else
      {
         $fields_str = $fields;
      }
      $fields_str = $fields_str . "&key=" . self::$key . "&secret=" . self::$secret;
      $id_str = ($id != '') ? "/$id" : "";
      $url = self::$base_url . $id_str;

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POST, count($fields));
      curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_str);
      if ($is_return_transfer == 1)
      {
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      }
      $result = curl_exec($ch);
      curl_close($ch);
      return $result;
   }

}

You can use this like YourClass::yourMethodName($id, $fields, $is_return_transfer);

Upvotes: 1

Related Questions