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

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

Stack Overflow用户
提问于 2021-10-11 06:57:50
回答 1查看 1.1K关注 0票数 1

我试图上传文件logo.png到皮纳塔IPFS使用curl和php。文档在javacript/Axios中,但正试图在php中完成。

Pinata Docs

当我运行下面的代码时,它会引发错误。

{"error":{"reason":"KEYS_MUST_BE_STRINGS","details":"pinata_api_key和pinata_secret_api_key必须都是字符串“}}

到目前为止,这里是编码

代码语言:javascript
复制
$url = "https://api.pinata.cloud/pinning/pinFileToIPFS"; // Where to upload file to
   $data = array(
       'pinata_api_key' => '00xxxxxx',
    'pinata_secret_api_key' => 'e1xxxxxxxxxxxxxxxxxxcccccccc');

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

//$data = new CURLFile($file, mime_content_type($file));
//file_get_contents();

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
echo $result = curl_exec($handle);

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

else {
  // $info = curl_getinfo($handle);
  // print_r($info);
  echo $result;
}


curl_close($handle);
EN

回答 1

Stack Overflow用户

发布于 2022-10-03 09:18:26

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

在ipfs中添加文件fpfs / Pin文件

圆满解决

代码语言:javascript
复制
 /** create curl file */
     $cFile = curl_file_create($_FILES['logo']['tmp_name'], $_FILES['logo']['type'], $_FILES['logo']['name']);
  /** metakey 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 */
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69522161

复制
相关文章

相似问题

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