
OpenSys
Novice
Apr 4, 2009, 4:59 PM
Post #1 of 10
(1035 views)
|
|
Upload with CURL FAIL
|
Can't Post
|
|
Hello, I trying to make a simple post file upload to one HTTP page using the CURL, But do not work at all. My code freezes the script and then i need to stop it with ctrl+c. I tested a identical script build in PHP that's works fine and upload my file. I need the do it with curl, because curl have options to limit the bandwidth. Please helps. My perl code: #!/usr/bin/perl use strict; use warnings; use File::stat; use WWW::Curl::Easy; use IO::File; use FileHandle; my $myfile = '/test.txt'; my @upload_headers = ( 'Connection: Keep-Alive', 'Keep-Alive: 300', 'Referer: ', 'Expect: ', 'Content-Type: multipart/form-data' ); my $user_agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20091107 Firefox/3.0'"; my $request_url = "http://www.mysite.net/upload/index.php"; sub builduploadfile { my ($file)=@_; my $fh = new FileHandle; if ($fh->open("< $file")) { undef $fh; } return $fh; } my @extra_vars = ( 'ligado'=> '1', 'file'=>$myfile ); #my $extra_vars2 = "ligado=1&file=$myfile"; my $curl = new WWW::Curl::Easy; $curl->setopt(CURLOPT_HEADER,0); $curl->setopt(CURLOPT_HTTPHEADER, \@upload_headers); $curl->setopt(CURLOPT_USERAGENT, $user_agent); $curl->setopt(CURLOPT_URL, $request_url); $curl->setopt(CURLOPT_POST, 1); $curl->setopt(CURLOPT_POSTFIELDS, \@extra_vars); $curl->setopt(CURLOPT_FAILONERROR, 1); $curl->setopt(CURLOPT_UPLOAD, 1); $curl->setopt(CURLOPT_INFILE, builduploadfile($myfile)); $curl->setopt(CURLOPT_INFILESIZE,length(builduploadfile($myfile))); $curl->setopt(CURLOPT_CUSTOMREQUEST, "POST"); $curl->setopt(CURLOPT_SSL_VERIFYPEER, 0); $curl->setopt(CURLOPT_SSL_VERIFYHOST, 0); $curl->setopt(CURLOPT_FOLLOWLOCATION, 1); #$curl->setopt(CURLOPT_AUTOREFERER, 1); $curl->setopt(CURLOPT_COOKIESESSION, 1); $curl->setopt(CURLOPT_COOKIEFILE, "cookiefile.txt"); $curl->setopt(CURLOPT_TIMEOUT,30); $curl->setopt(CURLOPT_VERBOSE, 1); my $response_body; open (my $fileb, ">", \$response_body); $curl->setopt(CURLOPT_WRITEDATA,$fileb); my $retcode = $curl->perform; my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE); if ($retcode == 0) { print("Transfer went ok ($response_code)\n"); } else { print("An error happened: ".$curl->strerror($retcode)." ($retcode)\n"); }
|