HTTP POST from PHP, without cURL

Written by on November 18, 2006 in Tips - 8 Comments

Wez have a new post on his blog on HTTP post from PHP without Curl. The code is using fopen() mixed with stream_context_create() to make post request, then stream_get_contents() to retrieve data. Headers are optional but if not set, Content-Type will be set automatically to application/x-www-form-urlencoded. Wez suggested also to look at the http_build_query() to assemble query/post parameters from a PHP vars.

I don’t think we do a very good job of evangelizing some of the nice things that the PHP streams layer does in the PHP manual, or even in general. At least, every time I search for the code snippet that allows you to do an HTTP POST request, I don’t find it in the manual and resort to reading the source. (You can find it if you search for “HTTP wrapper” in the online documentation, but that’s not really what you think you’re searching for when you’re looking).

8 Comments on "HTTP POST from PHP, without cURL"

  1. Armands Pucs December 18, 2006 at 11:46 am · Reply

    Hello!
    This function is super-useful :)
    But I can`t seem to understand how the $data var must be
    I tried to call the function like this: do_post_request(“http://www.a.com/test.php”, “cocobongo”);
    test.php is opened (I can see it in the log) but $_POST[content] is empty.
    I am probaby not understanding something basic about php, but could someone please post a working example on how the function must be called.
    Thanks in advance for help :)
    Armand from Latvia

  2. Tim October 18, 2007 at 10:01 pm · Reply

    I don’t think this code was ever tested by the writer. It does not work at all.
    -Tim

  3. Hatem October 19, 2007 at 11:14 am · Reply

    The code work fine Tim, you probably test it with the wrong PHP. Read again Wez’ post or ask him directly on his blog.

  4. x7ian January 9, 2008 at 3:40 pm · Reply

    Hallo! this seems to be a great code segment.
    Its just that the data dont seem to det the data to reach its destiny. I have a php script in a server that recives infromation from a form. This script attemps to send the data to another server by doing this:
    $data = $_POST;
    do_post_request(“http://www.torreblanca.com.gt/qlinspira”, $data );
    I beive this should work, or i may not be understanding how $data must be created.
    I was also thinking that maybe using some value in the $optional_headers variable but i dont know wich other value could that be.
    Coul someone trow some light on this one please?
    Any ideas??
    Tanx!
    – x7ian 8-)

  5. Alex September 8, 2008 at 4:18 pm · Reply

    To avoid wasting hours like I did – make sure ‘POST’ is in capital letters!

  6. Volvox July 22, 2009 at 5:00 am · Reply

    To correctly prepare data you have to use: http_build_query
    Try this example:
    $data = array(‘user’=>’Volvox’,
    ‘pass’=>’IlovePHP’,
    ‘remember’=>’yes’);
    $data = http_build_query($data);

    Sadly this
    do_post_request function does not work for me :(
    I have tested it on PHP 5.2.8 as URL giving full adress (eg. http://test.com/test.php) and short (test.php) but both do nothing. Function ends and nothing happens.

  7. Volvox July 22, 2009 at 6:20 am · Reply

    Ok, it works! (sadly not as i expected)
    This function returns requested page as a string. Here is example how to use it:

    $data = http_build_query(array(‘msg’=>$message));
    $response = post_request(‘login.php’, $data);
    echo(“Status = ” . $response);
    }

  8. jules345 June 3, 2010 at 9:56 am · Reply

    Here is a working php example
    $url = “http://www.url.com/postToMe.php”;
    $data = “Field1=test&Field2=test2&Field3=test3″;
    $response = do_post_request($url, $data);
    echo $response;
    In your postToMe.php script you could have,
    $one = $_POST[‘Field1′];
    $two = $_POST[‘Field2′];
    $three = $_POST[‘Field3′];
    return “$one$two$three”;
    So you script would output “testtest2test3″;
    enjoy,
    Jules345

Leave a Comment