-
Notifications
You must be signed in to change notification settings - Fork 120
/
ipfs.class.php
106 lines (77 loc) · 2.83 KB
/
ipfs.class.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
<?php
/*
IPFS api binding in PHP
Copyright (C) 2015 IpfsPics 2015
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class IPFS {
private $gatewayIP;
private $gatewayPort;
private $gatewayApiPort;
function __construct($ip, $port, $apiPort) {
$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;
}
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 . " a831rwxi1a3gzaorw1w2z49dlsor");
}
$output = curl_exec($ch);
if ($output == FALSE) {
//todo: when ipfs doesn't answer
}
curl_close($ch);
return $output;
}
}