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

我正在尝试使用 curl 和 php 将文件 logo.png 上传到 Pinata IPFS。文档在 javacript/Axios 中,但我试图在 php 中进行

Pinata Docs

当我运行下面的代码时,它会抛出错误

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

到目前为止,这里是编码

$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);
stack overflow How to properly upload files to pinata ipfs using curl php
原文答案

答案:

作者头像

**I have successfully pinning file in IPFS please use this.**

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

成功解决

 /** 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 */