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

0mkxixxg  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(270)

我尝试通过PHP将数据存储到ipfs,我使用curl与API通信,它在我的本地节点上工作正常,但我想使用www.example.com中的外部节点infura.io
但是由于某种原因,ipfs.infura.io拒绝我通过php连接,即使是一个简单的命令,比如...我已经在我的本地主机和几个服务器上试过了
下面是一个简单的端点,您可以在浏览器中打开它并获得输出
https://ipfs.infura.io:5001/api/v0/pin/add?arg=QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn
但是当我试图通过PHP打开它时,我得到了
无法连接到ipfs.infura.io端口5001:连接被拒绝
或使用其他方法(如file_get_contents)时
如果您有任何问题,请通过以下方式解决:无法打开流:连接被拒绝
我在本地主机和多个服务器上试过了,即使通过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);
628mspwn

628mspwn1#

为了连接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);

相关问题