Home

php-example.php

Download
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

$username = "joeuser";
$password = "pass123";
$filename = "filename.txt";
$file_id = 12345;

//print upload_file($username, $password, $filename);
//print retrieve_files($username, $password);
//print retrieve_file($username, $password, $file_id);
//print delete_file($username, $password, $file_id);


/**
 * Upload a file to Fileslap under $username's account.
 */
function upload_file($username, $password, $filename) {
  $filename = "filename.txt";
  $postvars = array('file' => "@" . $filename);
  $curl = curl_init();

  curl_setopt($curl, CURLOPT_URL, 'http://fileslap.com/api/files/');
  curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);  
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);

  $response = curl_exec($curl); // returns the JSON file data
  curl_close ($curl);
  return $response;
}


/**
 * View file data for all of the user's files.
 */
function retrieve_files($username, $password) {
  $curl = curl_init();

  curl_setopt($curl, CURLOPT_URL, 'http://fileslap.com/api/files/');
  curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);  
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  $response = curl_exec($curl);
  curl_close ($curl);
  return $response;
}


/**
 * View file data for a single file, by passing a file ID.
 */
function retrieve_file($username, $password, $file_id) {
  $curl = curl_init();

  curl_setopt($curl, CURLOPT_URL, 'http://fileslap.com/api/files/' . $file_id . '/');
  curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);  
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  $response = curl_exec($curl);
  curl_close ($curl);
  return $response;
}


/**
 * Delete a single file, by passing a file ID.
 */
function delete_file($username, $password, $file_id) {
  $curl = curl_init();

  curl_setopt($curl, CURLOPT_URL, 'http://fileslap.com/api/files/' . $file_id . '/');
  curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
  curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);  
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  $response = curl_exec($curl);
  curl_close ($curl);
  return $response;
}