tags:

views:

452

answers:

5

Hi, I'm setting up a custom e-commerce solution, and the payment system I'm using requires me to send HTTPS POSTS.

How can I do this using php (and CURL?), is it any different from sending http posts?

UPDATE:

Thanks for your replies, they've been very useful. I assume I will need to purchase an SSL certificate for this to work, and I will obviously do this for the final site, but is there any way for me to test this without buying one?

Thanks, Nico

+1  A: 

If you are using curl, you can pass in the -d switch for your parameters. This results in using an HTTP post. Something like

curl http://foo.com -d bar=baz -d bat=boo

would result in an HTTP post to http://foo.com with the appropriate parameters

Rob Di Marco
note the S in HTTPS
Milan Babuškov
cURL'll do fine with HTTPS.
ceejayoz
+1  A: 

No, there is no much difference. Curl does everything necessary itself.

See the examples in the user comments on the curl_setopt reference page how it’s done.

Gumbo
A: 

Similar question: POST to URL with PHP and Handle Response

Using the accepted solution (Snoopy PHP Class), you can do something like the following:

<?php

  $vars = array("fname"=>"Jonathan","lname"=>"Sampson");
  $snoopy = new Snoopy();

  $snoopy->httpmethod = "POST";
  $snoopy->submit("https://www.somesite.com", $vars);
  print $snoopy->results;

?>
Jonathan Sampson
+4  A: 

PHP/Curl will handle the https request just fine. What you may need to do, especially when going against a dev server, is turn CURLOPT_SSL_VERIFYHOST off. This is because a dev server may be self signed and fail the verify test.

$postfields = array('field1'=>'value1', 'field2'=>'value2');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://foo.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
A: 

You can also use the stream api and http/https context options

$postdata = http_build_query(
  array(
    'FieldX' => '1234',
    'FieldY' => 'yaddayadda'
  )
);

$opts = array(
  'http' => array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $postdata
  )
);
$context  = stream_context_create($opts);
$result = file_get_contents('http://...', false, $context);

You still need an extension that provides the ssl encryption. That can either be php_openssl or (if compiled that way) php_curl.

VolkerK