-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIPFS.php
139 lines (99 loc) · 3.38 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
This code is licensed under the MIT license.
See the LICENSE file for more information.
*/
namespace Cloutier\PhpIpfsApi;
class IPFS {
private $gatewayIP;
private $gatewayPort;
private $gatewayApiPort;
private $gatewayProtocol;
function __construct($ip = "localhost", $port = "8080", $apiPort = "5001", $protocol = "http") {
$this->gatewayIP = $ip;
$this->gatewayPort = $port;
$this->gatewayApiPort = $apiPort;
$this->gatewayProtocol= $protocol;
}
public function cat ($hash) {
$ip = $this->gatewayIP;
$port = $this->gatewayPort;
$protocol = $this->gatewayProtocol;
return $this->curl("$protocol://$ip:$port/ipfs/$hash");
}
public function add ($content) {
$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;
$protocol = $this->gatewayProtocol;
$req = $this->curl("$protocol://$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;
$protocol = $this->gatewayProtocol;
$response = $this->curl("$protocol://$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;
$protocol = $this->gatewayProtocol;
$response = $this->curl("$protocol://$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;
$protocol = $this->gatewayProtocol;
$response = $this->curl("$protocol://$ip:$port/api/v0/pin/add/$hash");
$data = json_decode($response, TRUE);
return $data;
}
public function pinRm ($hash) {
$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;
$protocol = $this->gatewayProtocol;
$response = $this->curl("$protocol://$ip:$port/api/v0/pin/rm/$hash");
$data = json_decode($response, TRUE);
return $data;
}
public function version () {
$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;
$protocol = $this->gatewayProtocol;
$response = $this->curl("$protocol://$ip:$port/api/v0/version");
$data = json_decode($response, TRUE);
return $data['Version'];
}
public function id () {
$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;
$protocol = $this->gatewayProtocol;
$response = $this->curl("$protocol://$ip:$port/api/v0/id");
$data = json_decode($response, TRUE);
return $data;
}
private function curl ($url, $data = "") {
$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 ($data != "") {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; boundary=a831rwxi1a3gzaorw1w2z49dlsor'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "--a831rwxi1a3gzaorw1w2z49dlsor\r\nContent-Type: application/octet-stream\r\nContent-Disposition: file; \r\n\r\n" . $data . "\r\n--a831rwxi1a3gzaorw1w2z49dlsor");
}
$output = curl_exec($ch);
if ($output == FALSE) {
//todo: when ipfs doesn't answer
}
curl_close($ch);
return $output;
}
}