HTTP POST with PHP without cURL

Awesome PHP

a great php idea!
It’s strange how powerful PHP can be. There’s a legend that cURL is the only way to perform a HTTP POST with PHP, but that isn’t the truth. An extremely useful script is by using stream_context_create:

$optional_headers = null;
$params = array('http' => array(
                'method' => 'POST',
                'content' => http_build_query(array('name' => 'my-name'))));
 
if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
}
 
$ctx = stream_context_create($params);
$fp = @fopen('http://example.com/post.php', 'rb', false, $ctx);

Well this is only a snippet. You can change a lot this code and perform any request, but here’s a small start up. However note that this will do the same as while posting to example.com/post.php via web form!

4 thoughts on “HTTP POST with PHP without cURL

  1. And to post a file how to do it ??? (ok the optional header will be multipart/form data but where to put the contentof the file to be posted )

Leave a Reply

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