Create POST requests with PHP Headers

If for some reason you need to create a POST request using PHP headers, this is the way to go:

//The domain of your website
$host = 'www.yoursite.com';
//The path you want to send the request to: (www.yoursite.com/user/add)
$path = "/user/add";
//Encode the POST params you want to send
$data = urlencode("username=John&surname=smith&phone=0856474633");

header("POST $path HTTP/1.1\r\n" );
header("Host: $host\r\n" );
header("Content-type: application/x-www-form-urlencoded; charset=UTF-8\r\n" );
header("Content-length: " . strlen($data) . "\r\n" );
header("Connection: close\r\n\r\n" );
header($data);

Leave a Reply

Your email address will not be published. Required fields are marked *