HTTP_Request2
[ class tree: HTTP_Request2 ] [ index: HTTP_Request2 ] [ all elements ]

Source for file upload-rapidshare.php

Documentation is available at upload-rapidshare.php

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

Documentation generated on Mon, 11 Mar 2019 15:56:03 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.