Sending data over websites using HTTP and receiving over HTTPS

I want to send an encrypted variable from a website on an HTTP page to another website on an HTTPS page, the code I use for this is:

$VARIABLE = 'myvariable';

function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

do_post_request('https://localhost/myphpfile.php', $VARIABLE);

      

This works fine with HTTP, but not HTTPS, but I think it's only because I'm on a local server running WAMP that forces the connection to be dropped.

Anyway, my question is, what do I need in 'myphpfile' to get the data passed to it?

Thanks in advance!

UPDATE: Wow, thanks for all the quick answers! As many of you have guessed, I just looked through cURL and found this on Google:

$url = 'https://localhost/myphpfile.php';

// Initialize session and set URL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);

      

I know that the VERIFYHOST parameter causes it to not validate a valid certificate, but I think I will just wait until I leave the testing server, then I will get it, but could you please tell me how I can get the data sent to 'myphpfile'?

+3


source to share


3 answers


Based on your cURL code, you really just need to add the following lines before the call curl_exec()

:



// number of POST variables you're passing - your number will likely be different
curl_setopt($ch,CURLOPT_POST,3);
// the post variables themselves - make sure the above number matches the number
// of fields here
curl_setopt($ch,CURLOPT_POSTFIELDS,"field1=foo&field2=bar&field3=baz");

      

+1


source


Is WAMP (Windows?!?) Listening for SSL connections on port 443? If this is the case, and you have your web server configured to properly serve scripts in a manner similar to your HTTP site, then it should just work.



You also need to jump over the hoops to make sure PHP validates the server certificate. If you don't, you are basically enabling a man-in-the-middle attack, defeating the whole point of SSL usage. cURL can help with this.

0


source


You need to configure the SSL based virtual server in the httpd.conf appache to listen on port 443. You also need to create an SSL certificate for your server.

There are many tutorials on the internet to troubleshoot SSL certificate and configure Apache SSL based virtual server

0


source







All Articles