Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 622
  • Last Modified:

CURL PUT of an image

I need to perform the PUT to a remote server using PHP; I thought of using CURL, but for some reason I am unable to perform a simple put.

I am perfectly able tp specify the headers (including the authentication cookie), but for some reason the POST body is not present - I get an "Expect: 100-continue", but then the server closes the connection immediately and I cannot send it (which causes an HTTP/1.1 411 Length Required).

Can I perform a PUT using CURL of an image file, without separating the image in several files?
0
Emanuele_Ciriachi
Asked:
Emanuele_Ciriachi
  • 4
  • 4
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
Most web servers will not accept a 'PUT' command, usually considered a security problem.  On the public internet, usually only GET, POST, and HEAD are supported in web server for security reasons.
0
 
Ray PaseurCommented:
You might be able to move the image with FTP.  I agree with Dave about PUT - it's not well thought of.
0
 
Emanuele_CiriachiAuthor Commented:
Our application servers communicate with PUT within our private internal network, and this particular one _demands_ a PUT.

What is the simplest way to obtain it, without using any odd, external Php library?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
I can show you a CURL POST example.  I do not have a teaching sample of a PUT, but maybe you can adapt this.
<?php // RAY_curl_post_example.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO USE CURL POST TO START AN ASYNCHRONOUS PROCESS


function curl_post($url, $post_array, $timeout=2, $error_report=FALSE)
{
    // PREPARE THE POST STRING
    $post_string = '';
    foreach ($post_array as $key => $val)
    {
        $post_string .= urlencode($key) . '=' . urlencode($val) . '&';
    }
    $post_string = rtrim($post_string, '&');

    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url         );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE        );
    curl_setopt( $curl, CURLOPT_POST,           TRUE         );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout     );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE         );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}


// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$args["name"]  = 'Ray';
$args["email"] = 'Ray.Paseur@Gmail.com';

// ACTIVATE THIS TO SEE THE ARRAY OF ARGS
// var_dump($args);

// SET THE URL
$url = "http://LAPRBass.com/RAY_bounce_post.php";

// CALL CURL TO POST THE DATA
$htm = curl_post($url, $args, 3, TRUE);

// SHOW WHAT CAME BACK, IF ANYTHING
if ($htm)
{
	echo "<pre>";
	echo htmlentities($htm);
}
else
{
    echo "NO RESPONSE YET FROM $url -- MAYBE BECAUSE IT IS RUNNING ASYNCHRONOUSLY";
}

Open in new window

0
 
Emanuele_CiriachiAuthor Commented:
I have been trying to adapt a CURL POST all the afternoon, with no luck - sorry, that won't do.

The reason why it doesn't work (even using curl_setopt ( ..., CURLOPT_CUSTOMREQUEST, ....) ) is that, for some reason, the binary data of the image is sent in a separate parts: the first part goes in, but the server requires everything in one POST and closes the connection, effectively not listening and causing the PUT to fail.

I tried, but could not find a way to let CURL understand that it should send everything in one go.
0
 
Ray PaseurCommented:
Have you tried to base64_encode the binary data?
http://us.php.net/manual/en/function.base64-encode.php
0
 
Emanuele_CiriachiAuthor Commented:
I didn't, will try tomorrow, but I doubt it will be the cause.
0
 
Ray PaseurCommented:
I thought about this a little more over night.  Here is what I would do if it were my responsibility to get an image from one of my servers onto my other server.

Create a secret key and make an md5() string of the key and the URL of the image, like this:

$url = 'http://path/to/image';
$key = 'My Secret Key';
$md5 = md5($key . $url);

Make a GET method request to the other server with url = $url and md5 = $md5.  The other server, which also knows the secret key, can use the URL to reconstruct the string and run its own md5() function.  If the md5 string that was sent matches the md5 string on the other server you can use the URL that was sent.  There are other ways to do this, perhaps with a cross-post handshake like PayPal, but the md5 method is probably OK for most uses.  Since your other server is confident that it has a legitimate URL, it can use file_get_contents() or CURL GET to read the image from the URL.  If security is a concern, you might start the security layers by making all the requests via HTTPS.

HTH, ~Ray
0
 
Emanuele_CiriachiAuthor Commented:
I have eventually been able to perform a PUT using CURL; I juse needed to experiment more with CURL configuration parameters, in particular, CURLOPT_CUSTOMREQUEST should be set to "PUT", and the Content-length should not have been sent more than once (ooops).
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now