cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

PHP, PUT and cURL to another server - Answered

From: Julian Bond <julian_bond_at_voidstar.com>
Date: Thu, 8 Jun 2006 12:46:26 +0100

Julian Bond <julian_bond_at_voidstar.com> Thu, 8 Jun 2006 12:15:52
>I'm having trouble making PUT file work with cURL from PHP to another
>server.

To answer my own question, After thrashing around and reading the php
manual I've fixed it. Hope this helps others.

To PUT a file from PHP

$url = "http://some.server.com/put_script";
$localfile = "localfile.csv";

$fp = fopen ($localfile, "r");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'user:password');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));

$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);

curl_close($ch);
fclose($fp);

print $http_code;
print "<br /><br />$http_result";
if ($error) {
   print "<br /><br />$error";
}

Note that some of these parameters are old names. And that
CURLOPT_INFILE takes a file handle opened for read, not a file name. The
comments in the example here
http://curl.haxx.se/libcurl/php/examples/ftpupload.html about file size
are answered. just use
filesize($filename);

-- 
Julian Bond  E&MSN: julian_bond at voidstar.com  M: +44 (0)77 5907 2173
Webmaster:           http://www.ecademy.com/     T: +44 (0)192 0412 433
Personal WebLog:     http://www.voidstar.com/    skype:julian.bond?chat
                     *** Just Say No To DRM ***
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2006-06-08