| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Usage example for HTTP_Request2 package: uploading a file to rapidshare.com
|
|
|
4 |
*
|
|
|
5 |
* Inspired by Perl usage example: http://images.rapidshare.com/software/rsapi.pl
|
|
|
6 |
* Rapidshare API description: http://rapidshare.com/dev.html
|
|
|
7 |
*
|
|
|
8 |
* $Id: upload-rapidshare.php 287307 2009-08-14 15:40:22Z avb $
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
require_once 'HTTP/Request2.php';
|
|
|
12 |
|
|
|
13 |
// You'll probably want to change this
|
|
|
14 |
$filename = '/etc/passwd';
|
|
|
15 |
|
|
|
16 |
try {
|
|
|
17 |
// First step: get an available upload server
|
|
|
18 |
$request = new HTTP_Request2(
|
|
|
19 |
'http://rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver_v1'
|
|
|
20 |
);
|
|
|
21 |
$server = $request->send()->getBody();
|
|
|
22 |
if (!preg_match('/^(\\d+)$/', $server)) {
|
|
|
23 |
throw new Exception("Invalid upload server: {$server}");
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
// Calculate file hash, we'll use it later to check upload
|
|
|
27 |
if (false === ($hash = @md5_file($filename))) {
|
|
|
28 |
throw new Exception("Cannot calculate MD5 hash of '{$filename}'");
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
// Second step: upload a file to the available server
|
|
|
32 |
$uploader = new HTTP_Request2(
|
|
|
33 |
"http://rs{$server}l3.rapidshare.com/cgi-bin/upload.cgi",
|
|
|
34 |
HTTP_Request2::METHOD_POST
|
|
|
35 |
);
|
|
|
36 |
// Adding the file
|
|
|
37 |
$uploader->addUpload('filecontent', $filename);
|
|
|
38 |
// This will tell server to return program-friendly output
|
|
|
39 |
$uploader->addPostParameter('rsapi_v1', '1');
|
|
|
40 |
|
|
|
41 |
$response = $uploader->send()->getBody();
|
|
|
42 |
if (!preg_match_all('/^(File[^=]+)=(.+)$/m', $response, $m, PREG_SET_ORDER)) {
|
|
|
43 |
throw new Exception("Invalid response: {$response}");
|
|
|
44 |
}
|
|
|
45 |
$rspAry = array();
|
|
|
46 |
foreach ($m as $item) {
|
|
|
47 |
$rspAry[$item[1]] = $item[2];
|
|
|
48 |
}
|
|
|
49 |
// Check that uploaded file has the same hash
|
|
|
50 |
if (empty($rspAry['File1.4'])) {
|
|
|
51 |
throw new Exception("MD5 hash data not found in response");
|
|
|
52 |
} elseif ($hash != strtolower($rspAry['File1.4'])) {
|
|
|
53 |
throw new Exception("Upload failed, local MD5 is {$hash}, uploaded MD5 is {$rspAry['File1.4']}");
|
|
|
54 |
}
|
|
|
55 |
echo "Upload succeeded\nDownload link: {$rspAry['File1.1']}\nDelete link: {$rspAry['File1.2']}\n";
|
|
|
56 |
|
|
|
57 |
} catch (Exception $e) {
|
|
|
58 |
echo "Error: " . $e->getMessage();
|
|
|
59 |
}
|
|
|
60 |
?>
|