试图通过 http api 将数据存储在外部 ipfs 节点上,ipfs.infura.io 拒绝我的连接

trying to store data on external ipfs node via http api , ipfs.infura.io refusing my connection

我正在尝试通过 PHP 将数据存储到 ipfs,我使用 curl 与 API 通信,它在我的本地节点上工作正常,但我想使用来自infura.io

但出于某种原因,ipfs.infura.io 拒绝通过 php 连接我 即使是一个简单的命令...我已经在我的本地主机和几台服务器上试过了

这是一个简单的端点,您可以在浏览器中打开它并获取输出

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

但是当我尝试通过 php 打开它时,我得到

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

或使用其他方法时 file_get_contents

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

我已经在本地主机和多个服务器上试过了,即使通过 ssh 命令行我也得到了相同的结果

知道为什么会这样吗?

这是我的代码的简化版本 n

    $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);

为了连接ipfs节点,你需要有一个客户端库。客户端库自动格式化 API 响应以匹配编程语言中使用的数据类型,并处理其他特定的通信规则。

在javascript,

import { create as ipfsHttpClient } from "ipfs-http-client";
const client = ipfsHttpClient("https://ipfs.infura.io:5001/api/v0");

然后这个客户提出请求:

const added = await client.add(file, {
        progress: (prog) => console.log(`received:${prog}`),

在php中,你可以使用这个包:https://github.com/cloutier/php-ipfs-api

勾选此项以与 infura 互动:https://github.com/digitalkaoz/php-ipfs-api

这个库需要 cURL 模块:

$ sudo apt-get install php5-curl
$ composer require cloutier/php-ipfs-api
$ composer install

  $ export IPFS_API=http://somehost:5001/api/v0

要从命令行使用此驱动程序,只需提供选项(或保留默认选项):

$ bin/php-ipfs version --driver=IPFS\Driver\Http
$ bin/php-ipfs version

客户端此驱动程序旨在以编程方式使用:

$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);