1

Am trying to upload files logo.png to Pinata IPFS using curl and php. The Documentation is in javacript/Axios but am trying to do it in php

Pinata Docs

When I run the code below, it throws error

{"error":{"reason":"KEYS_MUST_BE_STRINGS","details":"pinata_api_key and pinata_secret_api_key must both be strings"}}

here is coding so far

$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);
   
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38
  • Does this answer your question? [how to upload file using curl with PHP](https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php) – Justinas Oct 11 '21 at 07:01
  • 2
    According to the API documentation, `pinata_api_key` and `pinata_secret_api_key` must both be send as request headers, you tried to send them as parameters in the request body instead. – CBroe Oct 11 '21 at 07:11
  • Its the Header issue. As **CBroe** Suggested. I send `pinata_api_key` and `pinata_secret_api_key` as request headers and it works. – Nancy Moore Oct 12 '21 at 01:51

1 Answers1

0

I have successfully pinning file in IPFS please use this.

Add file fpfs / Pin file in ipfs

Solved successfully

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