PHP code example of fliq / ipfs

1. Go to this page and download the library: Download fliq/ipfs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fliq / ipfs example snippets


use Fliq\Ipfs\Ipfs;

$ipfs = new Ipfs('localhost', '8080', 'http', 'gateway');

$data = $ipfs->cat('QmSgvgwxZGaBLqkGyWemEDqikCqU52XxsYLKtdy3vGZ8uq')->wait(); // spaceship-launch.jpg


use Fliq\Ipfs\Ipfs;

$ipfs = new Ipfs('localhost', '5001', 'http', 'api');

$result = $ipfs->add('Hello, IPFS')->wait();

$cid = $result[0]['Hash']; // content identifier

use GuzzleHttp\Psr7\Utils;

$ipfs = new Ipfs('localhost', '5001', 'http', 'api');

$file = Utils::tryFopen('path/to/file.jpeg')

$results = $ipfs->add([
    'file.jpeg' => $file,
    'meta.json' => [
        'name' => 'My file',
        'description' => 'file description',
        'properties' => [...],
    ],
], ['wrap-with-directory' => true])->wait();

$directoryCid = $results[2]['Hash'];

$data = $ipfs->cat('Qm...')
               ->then(fn($stream) => $stream->getContents())
               ->then(fn($str) => json_decode($str, 1))
               ->wait();

// is the same as.

$data = $ipfs->get('Qm...')
             ->then(fn($str) => json_decode($str, 1))
             ->wait();

// is the same as

$data = $ipfs->json('Qm...')->wait();


use GuzzleHttp\Promise;

$promises = [];

$promises[] = $ipfs->add('Hello, IPFS');
$promises[] = $ipfs->cat('QmSgvgwxZGaBLqkGyWemEDqikCqU52XxsYLKtdy3vGZ8uq');

$responses = Promise\Utils::unwrap($promises);

$ipfs->call('name/resolve', ['query' => $args])->then(function(Response $response) {
    // handle response.
});