I'm trying to store data to ipfs via PHP, I use curl to communicate with API , it works fine on my local node, but I want to use an external node from infura.io
but for some reason, ipfs.infura.io is refusing my connection via php even a simple command like ... I've tried it on my localhost as well as a couple of servers
here is a simple endpoint that you can open in the browser and get the output
any idea why is this happening ?
here is a simplified version n of my code
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL,"https://ipfs.infura.io:5001/api/v0/pin/add?arg=QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn"); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_FAILONERROR, true); $res = curl_exec($curl); if (curl_errno($curl)) { $error_msg = curl_error($curl); echo ('error ...'); echo ($error_msg); exit(); } curl_close($curl); echo($res);CodePudding user response:
In order to connect
ipfsnode, you need to have a client library. client libraries automatically format API responses to match the data types used in the programming language and also handles other specific communication rules.In javascript,
import { create as ipfsHttpClient } from "ipfs-http-client"; const client = ipfsHttpClient("https://ipfs.infura.io:5001/api/v0");then this client makes request:
const added = await client.add(file, { progress: (prog) => console.log(`received:${prog}`),In php, you can use this package: https://github.com/cloutier/php-ipfs-api
Check this to interact with infura: https://github.com/digitalkaoz/php-ipfs-api
This library requires the cURL module:
$ sudo apt-get install php5-curl $ composer require cloutier/php-ipfs-api $ composer install $ export IPFS_API=http://somehost:5001/api/v0to use this Driver from the commandline simply provide the option (or leave it away since its the default):
$ bin/php-ipfs version --driver=IPFS\\Driver\\Http $ bin/php-ipfs versionClient this Driver is intended for programmatically usage:
$driver = $container[IPFS\Driver\Cli::class]; //$driver = $container[IPFS\Driver\Http::class]; $client = new IPFS\Client($driver); $response = $client->execute((new \IPFS\Api\Basics())->version()); var_dump($response);

