8

I found mediafire API few days ago.

http://developers.mediafire.com

and I search over the internet is there anyway to make a web app for upload files to mediafire account using API. Unfortunately I haven't found anything. Is anybody know how to create a file uploading web app with mediafire API and PHP.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Sasindu H
  • 1,538
  • 7
  • 24
  • 43
  • You gave an answer to your own question: http://developers.mediafire.com/index.php/REST_API and more specifically: http://developers.mediafire.com/index.php/REST_API#upload – Supericy Dec 27 '12 at 10:34

1 Answers1

13

First get a session token.

$apikey = 'YOUR API KEY HERE';
$appid = 'APPLICATIONID';
$email = 'your@email.com';
$passwd = 'PASSWORD';
$params = http_build_query(array(
   'email' => $email,
   'password'=> $passwd,
   'application_id' => $appid,
   'signature' => sha1("$email$passwd$appid$apikey"),
   'response_format' => 'json'
));
$fp = fopen('https://www.mediafire.com/api/user/get_session_token.php?'.$params, 'r');
$json = stream_get_contents($fp);
$obj = json_decode($json);
fclose($fp);

$session = $obj->response->session_token;

Now with this new $session key upload a file.

$filecontents = file_get_contents("/path/to/file");
$filesize = strlen($filecontents);
$opts = array(
  'http'=>array(
    'method'=>"POST",
    'header'=> "x-filename : ANYFILENAMEYOUWANT\r\n".
               "x-filesize : $filesize\r\n"
  )
);
$context = stream_context_create($opts);
$params = http_build_query(array(
    "session_token" => $session
));
$fp = fopen('http://www.mediafire.com/api/upload/upload.php?'.$params, 'r', false, $context);
fwrite($fp, $filecontents);
$result = stream_get_contents($fp);
fclose($fp);

Important Note: Please try it yourself. I have not tested it. Just saw the API and wrote this code. So it wont work on first go. You'll need to modify to make it work.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Hi, I'm a .NET developer, what's the `sha1` function? I'm trying to do it in .NET, but it doesn't work, here's my attempt: http://rextester.com/PVGYX5752 – Shimmy Weitzhandler Mar 10 '13 at 01:59
  • Hi, I'm a .NET developer, what's the sha1 function? I'm trying to do it in .NET, but it doesn't work, [here](http://rextester.com/PVGYX5752)'s my attempt. I posted a new question, please [take a look](http://stackoverflow.com/q/15329092/75500). – Shimmy Weitzhandler Mar 10 '13 at 23:43
  • It calculates `sha1` hash. – Shiplu Mokaddim Mar 11 '13 at 03:05
  • I wanted to know HOW it does it. Anyway I already got [my question](http://stackoverflow.com/q/15329092) answered so don't bother. – Shimmy Weitzhandler Mar 11 '13 at 10:24
  • good answer, but a few issues "$session = $json->response->session_token;" should be "$session = $obj->response->session_token;" "('your@email.compasswordAPPLICATIONID$apikey')," shouldn't have a $ before the api key – Matt Bucci Jun 01 '13 at 05:07
  • Thanks @MatthewBucci. Fixed those things. – Shiplu Mokaddim Jun 01 '13 at 10:44