msmp3 Posted July 30, 2007 Share Posted July 30, 2007 Hello, I want to know if it's possible to upload a file to www.rapidshare.com using a PHP script that resides on my site and then retrieve the URL of the uploaded file? Rapidshare offers a script to do this, but it's written in Perl and I do not know this language at all. Does someone know how to make this work in PHP, or somehow integrate that Perl Script into PHP so it can upload/retrieve uploaded URL? I have attached the Perl Script offered by Rapidshare. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Crow Posted July 30, 2007 Share Posted July 30, 2007 You're going to have to use sockets to achieve this (or dabble with the cURL libraries, I'm sure there's a function in there that can help) Looking at the perl script and what it does (in pseudo code): fetching http://rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver_v1 if ($login and $password) { print "Trying to upload to your premium account.\n" } else { print "Uploading as a free user.\n" } print "Uploading as filename '$filename'. Getting upload server infos.\n"; $socket = IO::Socket::INET->new(PeerAddr => "rapidshare.com:80") || die "Unable to open port: $!\n"; print $socket qq|GET /cgi-bin/rsapi.cgi?sub=nextuploadserver_v1 HTTP/1.0\r\n\r\n|; ($uploadserver) = <$socket> =~ /\r\n\r\n(\d+)/; unless ($uploadserver) { die "Uploadserver invalid? Internal error!\n" } print "Uploading to rs$uploadserver$uploadpath.rapidshare.com\n"; - Connect to http://rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver_v1 - Check for premium account (if you defined $login and $password) - Open a socket to rapidshare.com port 80 - GET (the URL above) - The URL returns a number which is set to $uploadserver. - Prints: Uploads to rs . $uploadserver . l3.rapidshare.com ---- IE: $uploadserver = 35 then it would upload to: rs35l3.rapidshare.com sysopen($fh, $file, O_RDONLY) || die "Unable to open file: $!\n"; $socket = IO::Socket::INET->new(PeerAddr => "rs$uploadserver$uploadpath.rapidshare.com:80") || die "Unable to open port: $!\n"; - Opens file as readonly (so fopen) - Opens a socket to the URL mentioned above $boundary = "---------------------632865735RS4EVER5675865"; $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="rsapi_v1"\r\n\r\n1\r\n|; if ($zone eq "prem" and $login and $password) { $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="login"\r\n\r\n$login\r\n|; $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="password"\r\n\r\n$password\r\n|; } if ($zone eq "col" and $login and $password) { $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="freeaccountid"\r\n\r\n$login\r\n|; $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="password"\r\n\r\n$password\r\n|; } $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="filecontent"; filename="$filename"\r\n\r\n|; $contenttail = "\r\n$boundary--\r\n"; $contentlength = length($contentheader) + $size + length($contenttail); $header = qq|POST /cgi-bin/upload.cgi HTTP/1.0\r\nContent-Type: multipart/form-data; boundary=$boundary\r\nContent-Length: $contentlength\r\n\r\n|; - Construct header for file upload based on account status. while ($cursize < $size) { $bufferlen = sysread($fh, $buffer, $maxbufsize, 0) || 0; unless ($bufferlen) { die "Error while sending data: $!\n" } print "$cursize of $size bytes sent.\n"; $cursize += $bufferlen; print $socket $buffer; } - Actual upload the file (going thru it bite by bite) ($result) = <$socket> =~ /\r\n\r\n(.+)/s; unless ($result) { die "Ooops! Did not receive any valid server results?\n" } print "$result >>> Verifying MD5...\n"; foreach (split(/\n/, $result)) { if ($_ =~ /([^=]+)=(.+)/) { $key_val{$1} = $2 } } -Receive the Results and store them to $result $result is the URL from what I understand ------------------------------------------- Shouldn't be too hard to make a PHP script to do it, most of what I did above was copy the comments and paste the code, and I tried to add whatever I could (with my knowledge of perl) Hope this helps, Steve Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.