1

I'm trying to upload a file to Pinata IPFS using curl and php.

I attempted to modify the existing code from the question below but I'm getting {"error":{"reason":"INVALID_ROUTE","details":"The provided route does not match a valid Pinata endpoint"}}

How to properly upload files to pinata ipfs using curl php

Here is the modified code from the link above:

$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);
Rob
  • 11
  • 2
  • Have you read all the comments in the thread you are referring to? – ino Feb 26 '22 at 09:51
  • @ino Yes, I saw the comment where it needs to be sent as a request header. I attempted to do so in the code above but am receiving that error. Is the implementation not correct? – Rob Feb 27 '22 at 17:09

1 Answers1

1

I have successfully pinning file in IPFS please use this.

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