8

几天前我发现了 mediafire API。

http://developers.mediafire.com

我在互联网上搜索是否有一个网络应用程序用于使用 API 将文件上传到 mediafire 帐户。不幸的是,我什么也没找到。有谁知道如何使用 mediafire API 和 PHP 创建文件上传网络应用程序。

4

1 回答 1

12

首先获取一个会话令牌。

$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;

现在用这个新$session密钥上传一个文件。

$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);

重要提示:自行尝试。我还没有测试过。刚刚看到API并写了这段代码。所以它不会在第一次运行时起作用。您需要进行修改以使其正常工作。

于 2012-12-27T10:51:32.017 回答