Saturday, January 25, 2014

POST request to https server php

1/25/2014

Use following PHP function to make a POST request to https server


<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

$sock = fsockopen("ssl://secure.yoursite.com", 443, $errno, $errstr, 30);
if (!$sock) die("$errstr ($errno)\n");

$data = "var1=" . urlencode("Value for var1") . "&var2=" . urlencode("Value for var2");

fwrite($sock, "POST /action.php HTTP/1.0\r\n");
fwrite($sock, "Host: secure.yoursite.com\r\n");
fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
fwrite($sock, "Accept: */*\r\n");
fwrite($sock, "\r\n");
fwrite($sock, $data);

$headers = "";
while ($str = trim(fgets($sock, 4096)))
$headers .= "$str\n";

echo "\n";

$body = "";
while (!feof($sock))
$body .= fgets($sock, 4096);

fclose($sock);

?>

helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top