PHP code example of ecourty / ipfs-php

1. Go to this page and download the library: Download ecourty/ipfs-php 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/ */

    

ecourty / ipfs-php example snippets




use IPFS\Client\IPFSClient;

// Three different ways to instantiate the client
$client = new IPFSClient(url: 'http://localhost:5001');

// If nothing is passed, the default values are used (localhost and 5001)
// $client = new IPFSClient();
// $client = new IPFSClient(host: 'localhost', port: 5001);

// Add a file
$file = $client->addFile('file.txt');

echo 'File uploaded: ' . $file->hash;
// File uploaded: QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks
// ...

// Get the file content
$fileContent = $client->cat($file->hash);
// ...

// Downloads the complete file
$file = $client->get($file->hash);
// ...

// Download the file as a tar archive (compression can be specified with the compression parameters)
$archive = $client->get($file->hash, archive: true);
file_put_contents('archive.tar', $archive);
// ...
shell
composer