Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return ipfs ref path instead of getting invalid index on $ipfs->size($hash); #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 41 additions & 47 deletions src/IPFS.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Cloutier\PhpIpfsApi;

use Exception;

class IPFS {
private $gatewayIP;
private $gatewayPort;
Expand All @@ -20,7 +22,7 @@ function __construct($ip = "localhost", $port = "8080", $apiPort = "5001") {
public function cat ($hash) {
$ip = $this->gatewayIP;
$port = $this->gatewayPort;
return $this->curl("http://$ip:$port/ipfs/$hash");
return $this->curl("http://$ip:$port/ipfs/$hash");

}

Expand All @@ -31,7 +33,7 @@ public function add ($content) {
$req = $this->curl("http://$ip:$port/api/v0/add?stream-channels=true", $content);
$req = json_decode($req, TRUE);

return $req['Hash'];
return $req['Hash'];
}

public function ls ($hash) {
Expand All @@ -56,7 +58,6 @@ public function size ($hash) {
}

public function pinAdd ($hash) {

$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;

Expand All @@ -65,65 +66,58 @@ public function pinAdd ($hash) {

return $data;
}

public function pinRm ($hash) {

$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;

$response = $this->curl("http://$ip:$port/api/v0/pin/rm/$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'];
}

public function id () {

$ip = $this->gatewayIP;
$port = $this->gatewayApiPort;

$response = $this->curl("http://$ip:$port/api/v0/id");
$data = json_decode($response, TRUE);
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"];
}

return $data;
}

private function curl ($url, $data = "") {
$ch = curl_init();
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 ($data != "") {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; boundary=a831rwxi1a3gzaorw1w2z49dlsor'));

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, "--a831rwxi1a3gzaorw1w2z49dlsor\r\nContent-Type: application/octet-stream\r\nContent-Disposition: file; \r\n\r\n" . $data . "\r\n--a831rwxi1a3gzaorw1w2z49dlsor");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}

$output = curl_exec($ch);

if ($output == FALSE) {
//todo: when ipfs doesn't answer
}
// 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;
}
}