-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathIPFS.php
123 lines (94 loc) · 3.06 KB
/
IPFS.php
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
This code is licensed under the MIT license.
See the LICENSE file for more information.
*/
namespace Cloutier\PhpIpfsApi;
use Exception;
class IPFS {
private $gatewayIP;
private $gatewayPort;
private $gatewayApiPort;
function __construct($ip = "localhost", $port = "8080", $apiPort = "5001") {
$this->gatewayIP = $ip;
$this->gatewayPort = $port;
$this->gatewayApiPort = $apiPort;
}
public function cat ($hash) {
$ip = $this->gatewayIP;
$port = $this->gatewayPort;
return $this->curl("http://$ip:$port/ipfs/$hash");
}
public function add ($content) {
$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;
$req = $this->curl("http://$ip:$port/api/v0/add?stream-channels=true", $content);
$req = json_decode($req, TRUE);
return $req['Hash'];
}
public function ls ($hash) {
$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;
$response = $this->curl("http://$ip:$port/api/v0/ls/$hash");
$data = json_decode($response, TRUE);
return $data['Objects'][0]['Links'];
}
public function size ($hash) {
$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;
$response = $this->curl("http://$ip:$port/api/v0/object/stat/$hash");
$data = json_decode($response, TRUE);
return $data['CumulativeSize'];
}
public function pinAdd ($hash) {
$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;
$response = $this->curl("http://$ip:$port/api/v0/pin/add/$hash");
$data = json_decode($response, TRUE);
return $data;
}
public function version () {
$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;
$response = $this->curl("http://$ip:$port/api/v0/version");
$data = json_decode($response, TRUE);
return $data["Version"];
}
private function curl ($url, $filepath=null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
if ($filepath !== null) {
// add the file
$cfile = curl_file_create($filepath, 'application/octet-stream', basename($filepath));
// post
$post_fields = ['file' => $cfile];
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
$output = curl_exec($ch);
// check HTTP response code
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$code_category = substr($response_code, 0, 1);
if ($code_category == '5' OR $code_category == '4') {
$data = @json_decode($output, true);
if (!$data AND json_last_error() != JSON_ERROR_NONE) {
throw new Exception("IPFS returned response code $response_code: ".substr($output, 0, 200), $response_code);
}
if (is_array($data)) {
if (isset($data['Code']) AND isset($data['Message'])) {
throw new Exception("IPFS Error {$data['Code']}: {$data['Message']}", $response_code);
}
}
}
// handle empty response
if ($output === false) {
throw new Exception("IPFS Error: No Response", 1);
}
curl_close($ch);
return $output;
}
}