curl-and-php
curl Upload: command line OK - libcurl/PHP problem!
Date: Fri, 02 Apr 2004 12:26:43 +0100
Dear fellow list member
curl Upload: command line OK - libcurl/PHP problem!
===================================================
I _can_ upload a file using the curl command line interface (curl CLI)
without any difficulty. However, I cannot find the way to do exactly
the same thing using the PHP interface to libcurl.
It seems that to do an Upload I have to set the CURLOPT_UPLOAD option,
but that doing this automatically sets the CURLOPT_PUT option, and I
then cannot either unset it or set (again) the CURLOPT_POST option.
The URL does not accept a "PUT" ...
Please see the commented PHP code below.
Any ideas? ... must me something silly that I am(/not) doing ...
Thanks in advance.
Harry
+-+-+-+-+.+-+-+-+-+.+-+-+-+-+.+-+-+-+-+.+-+-+-+-+.+-+-+-+-+.+-+-+-+-+.+-+-+-+-+-
http://www.eng.ox.ac.uk/people/Harry.Fearnley
Dept Engineering Science, Parks Rd, Oxford, OX1 3PJ, UK
Tel: +44 (0)1865 273928 -- Fax: +44 (0)1865 273010
================================================================================
<?php
# Do the actual Upload!
### We are trying to mimic the effect of a curl command line that
_does_ work:
### curl -v --insecure -b c2.txt -c c3.txt -o out.html -F
file=@test.PDF -F
### submit=Send%20Now https://site.ox.ac.uk/upload.html
###
### The above automatically does a "POST", and uploads the 4 Mb+ PDF file
###
### The output "out.html" will display as just "OK".
# First attempt to use libcurl from PHP to do a Weblearn Upload
# In a previous script we have done a logon to the server, supplied a
# username/password, and obtained a Cookie in the file "cookieFileName"
chdir("PDF/ENG2003");
# we need to be in the immediately containing dir of the source file
to avoid
# complaints (about "/" in filename) when we upload the file ...
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookieFileName");
curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookieFileName");
curl_setopt($ch, CURLOPT_URL, "https://site.ox.ac.uk/upload.html");
# We disguise the real URL when discussing the script.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); # Equivalent to -k or
--insecure
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); # maybe needs to be set
to 0 or 1
# (default 2) if CURLOPT_SSL_VERIFYPEER is set to 0 -- to get full
effect of -k
curl_setopt($ch, CURLOPT_VERBOSE, 1 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); # later, result -> local
variable
$result = curl_setopt($ch, CURLOPT_POST, 1);
#$result = curl_setopt($ch, CURLOPT_PUT, 1); # Doing a PUT -- needs
the 2 lines below
#$result = curl_setopt($ch, CURLOPT_GET, 1);
$filename = "test.PDF";
if (!$handle = fopen($filename, 'r')) {
echo "Cannot open file ($filename)\n";
exit;
}
$result = curl_setopt($ch, CURLOPT_INFILE, $handle );
$result = curl_setopt($ch, CURLOPT_INFILESIZE, filesize( $filename ) );
# If we do not specify the CURLOPT_INFILESIZE then there will probably
be a
# delay after reading in file, waiting for a timeout ...
#$result = curl_setopt($ch, CURLOPT_UPLOAD, 1); # Prepare for an upload
# Setting this _seems_ to set CURLOPT_PUT to TRUE !!! ... a "PUT .."
line
# is generated, which in turn leads
# to problems! -- we get the message:
# 405 HTTP method PUT is not supported by this URL
# NOT setting this means that no attempt is made to upload the file --
the
# progress meter shows this. The only advantage is that the generated
line
# is "POST ... " ...
# After setting CURLOPT_UPLOAD to 1, ...
# Even if we try to set CURLOPT_POST to 1 here, as well as
# CURLOPT_PUT to 0 -- nothing changes! We still generate a "PUT"
command!
curl_setopt($ch, CURLOPT_VERBOSE, 1); # to report everything
curl_setopt($ch, CURLOPT_NOPROGRESS, 0); # to show progress meter
...
#curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.10.6
(i386-redhat-linux-gnu) libcurl/7.10.6 OpenSSL/0.9.7a ipv6 zlib/1.1.4");
# above obtained from running the curl command line, where it supplied
# automatically, and is not specified on the command line.
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/6.0");
# Use a User-Agent that the Web server knows about, and seems happy
with!
# We must supply a browser name (? and version) -- if we do not, then
the
# server kicks us off -- suspecting us of being an unwelcome robot ...
$result = curl_setopt($ch, CURLOPT_POSTFIELDS,
"file=test.PDF&submit=Send%20Now");
# We authenticated via log on using the previous scripts, so now
# simply do the upload ...
$buffer = curl_exec ($ch); // execute the curl command
curl_close ($ch);
unset($ch);
echo "<PRE>" . htmlentities($buffer) . "<BR>\n";
?>
Received on 2004-04-02