Serverside PUT Using PHP

It is possible to do an HTTP put, server side, in PHP without using a PECL extension or curl. This code is far from perfect but it shows how to accomplish the task.

function httpPut($url, $data, $optional_headers = null)
    $params = array('http' => array(
                        'method' => 'PUT',
                        'header'  => 'Content-Type: application/octet-stream',
                        'content' => $data
    if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
    $ctx = stream_context_create($params);
    $fp = @fopen($url, 'rb', false, $ctx);
    if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
    $response = @stream_get_contents($fp);
    if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
    return $response;
This entry was posted in Code, Geek. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s