首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用curl php将文件上传到pinata ipfs

如何使用curl php将文件上传到pinata ipfs
EN

Stack Overflow用户
提问于 2022-02-26 07:51:58
回答 1查看 413关注 0票数 1

我正试图使用curl和php将一个文件上传到Pinata IPFS。

我试图从下面的问题修改现有代码,但我得到了{"error":{"reason":"INVALID_ROUTE",“详细信息”:“提供的路由与有效的Pinata端点不匹配”}

How to properly upload files to pinata ipfs using curl php

下面是上面链接中的修改代码:

代码语言:javascript
复制
$url = "https://api.pinata.cloud/pinning/pinFileToIPFS";

$boundary = uniqid();
$delimiter = '-------------' . $boundary;

$data = array(
'Content-Type' => 'multipart/form-data; boundary=' . $delimiter,
'pinata_api_key' => 'xxxxxxxx',
'pinata_secret_api_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx');

$file = 'logo.jpg';
$data['file'] = $file;

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, $data);
echo $result = curl_exec($handle);

if (curl_errno($handle)) {
  echo "CURL ERROR - " . curl_error($handle);
}
else {
  echo $result;
}

curl_close($handle);

EN

回答 1

Stack Overflow用户

发布于 2022-10-03 09:01:51

我已经成功地将文件固定在IPFS中,请使用这个.

代码语言:javascript
复制
     add file fpfs / Pin file in ipfs

  /** create curl file */
  $cFile = curl_file_create($_FILES['logo']['tmp_name'], $_FILES['logo']['type'], $_FILES['logo']['name']);

  /** meta-key and meta-values */
  $keyvalues = [
    'company' => 'BDTASK',
    'background' => '100% Trait',
    'Color' => 'RED',
  ];
  /** metadata array */
  $metadata = [
    'name' => 'This is test file',
    'keyvalues' => $keyvalues,
  ];

  /** post data array */
  $post = array(
    'file' => $cFile,
    'pinataMetadata' => json_encode($metadata)
  );

  /** header info pinata jwt authentication */
  $headers = array();
  $headers[] = 'Authorization: Bearer Pinata-JWT';
  
  $url = "https://api.pinata.cloud/pinning/pinFileToIPFS";       

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

  $result = curl_exec($ch);
  if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
  }
  curl_close($ch);
  print_r($result); /** Found IPFS CID in here */
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71274957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档