How to make a post request without curl?

You could use file_get_contents().

The PHP manual has a nice example here. This is just copy past from the manual:

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
$context  = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);

you can use stream_context_create and file_get_contents

<?php
$context_options = array (
        'http' => array (
            'method' => 'POST',
            'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
                . "Content-Length: " . strlen($data) . "\r\n",
            'content' => $data
            )
        );
?>


$context = stream_context_create($context_options); 
$result = file_get_contents('http://www.php.net', false, $context);

You can implement it yourself via sockets:

    $url = parse_url(''); // url
    $requestArray = array('var' => 'value');
    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_connect($sock, $url['host'], ((isset($url['port'])) ? $url['port'] : 80));
    if (!$sock) {
        throw new Exception('Connection could not be established');
    }

    $request = '';
    if (!empty($requestArray)) {
        foreach ($requestArray as $k => $v) {
            if (is_array($v)) {
                foreach($v as $v2) {
                    $request .= urlencode($k).'[]='.urlencode($v2).'&';
                }
            }
            else {
                $request .= urlencode($k).'='.urlencode($v).'&';
            }
        }
        $request = substr($request,0,-1);
    }
    $data = "POST ".$url['path'].((!empty($url['query'])) ? '?'.$url['query'] : '')." HTTP/1.0\r\n"
    ."Host: ".$url['host']."\r\n"
    ."Content-type: application/x-www-form-urlencoded\r\n"
    ."User-Agent: PHP\r\n"
    ."Content-length: ".strlen($request)."\r\n"
    ."Connection: close\r\n\r\n"
    .$request."\r\n\r\n";
    socket_send($sock, $data, strlen($data), 0);

    $result = '';
    do {
        $piece = socket_read($sock, 1024);
        $result .= $piece;
    }
    while($piece != '');
    socket_close($sock);
    // TODO: Add Header Validation for 404, 403, 401, 500 etc.
    echo $result;

Of course you have to change it to fill your needs or wrap it into a function.

Tags:

Php

Http