2

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

https://ipfs.infura.io:5001/api/v0/pin/add?arg=QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn

but when I try to open it via php i get

Failed to connect to ipfs.infura. io port 5001: Connection refused

or when using another method like file_get_contents

file_get_contents(ipfs.infura.io:5001/api/v0/pin/add?arg=QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn): failed to open stream: Connection refused

i've tried it on local host and multiple server , i get the same result even via ssh command line

enter image description here

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);
TylerH
  • 20,799
  • 66
  • 75
  • 101
hretic
  • 999
  • 9
  • 36
  • 78
  • Can you do this on the commandline with `curl`? What about the various options you set, do they make a difference? Without checking the docs, in particular a timeout of zero seems suspicious. Have you checked for errors there? Have you logged the actual communication using network sniffer? – Ulrich Eckhardt Jan 29 '22 at 12:15
  • 1
    Sounds like a support question for the maintainers of this API. – miken32 Jan 31 '22 at 20:29
  • can you add your request payload? – Mtxz Feb 01 '22 at 13:39
  • @UlrichEckhardt i get connection refused when using command line with curl as well , please check out the update at the end of my question , i found an address which accepts request body as GET parameter – hretic Feb 01 '22 at 15:44
  • 2
    So, in summary it has nothing to do with Laravel, PHP or web3, so you can remove those tags. It would also help if you didn't use images of text but the plain text. That said, I just ran curl against the URL you have in your question and received a response. I wouldn't rule out that it just blocks your IP address after too many attempts or some other networking issues. – Ulrich Eckhardt Feb 01 '22 at 16:55
  • So your example code does not work on your local env? I just tested it and I have the same response as when accessing the URL with browser, also using curl CLI. If so, it may be a firewall issue on your local? – Mtxz Feb 01 '22 at 18:10
  • sounds to me like a firewall issue, you're probably behind a firewall blocking connections on uncommon ports (like, say, port 5001) , what do you get with `curl -v 'https://ipfs.infura.io:5001/api/v0/pin/add?arg=QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn'` ? – hanshenrik Feb 02 '22 at 14:06

1 Answers1

3

In order to connect ipfs node, 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/v0

to 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 version

Client 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);
Yilmaz
  • 35,338
  • 10
  • 157
  • 202