简体   繁体   中英

Pinning a folder to Pinata (IPFS) using PHP doesn't work but individual files do

I have a script that uploads files to Pinata using cURL & PHP, it works great. But now im trying to pin an entire folder of files. I followed the little instructions they give but get -

[error] => Invalid request format.

Instructions

https://docs.pinata.cloud/pinata-api/pinning/pin-file-or-directory

It says to append files to an array which I do, but it wants the key of the array to be "file" and the array cant have the same key value for multiple files.

Here is my array -

Array
(
    [file] => Array
        (
            [0] => CURLFile Object
                (
                    [name] => 1662838796/playlist0.ts
                    [mime] => 
                    [postname] => 
                )

            [1] => CURLFile Object
                (
                    [name] => 1662838796/playlist1.ts
                    [mime] => 
                    [postname] => 
                )

            [2] => CURLFile Object
                (
                    [name] => 1662838796/playlist2.ts
                    [mime] => 
                    [postname] => 
                )

        )

)

In order to keep "File as the main index I added int keys to each file but still no luck. If I use this single value array it pins the file and works as expected. -

Array
(
    [file] => CURLFile Object
        (
            [name] => 1662838796/playlist2.ts
            [mime] => 
            [postname] => 
        )

)

Any idea where im going wrong here?

cURL Request -

$files = [
    '1662838796/playlist0.ts',
    '1662838796/playlist1.ts',
    '1662838796/playlist2.ts',
];

foreach ($files as $file) {
    $postData['file'] = curl_file_create($file);
}

$url = "https://api.pinata.cloud/pinning/pinFileToIPFS";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$json = json_decode($result, true); 

// returned CID
$hash = $json['IpfsHash'];

** Edit If I remove the "file" and just use index keys I get -

[error] => Unexpected field

Not sure if thats going in the right direction or wrong direction but its a different error so might help find the issue.

I have the request header set "Content-Type: application/json", Because you have that header the $postData will go to the BODY and the $_POST global will have nothing. The word array may turn up in the BODY.

Either you remove the JSON header, or you change the $postData to JSON.
Which depends what the app wants.
Try one and if it does not work try the other.

The easy one would be to remove the content type form $headers. Do not send any content type.
When not content type is specified when curl sees an array in the post data, it will change the content type to application/x-www-form-urlencoded

END OF UPDATE



I see you have curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); Do you have the HTTP request header have a "Content-Type: application/json"?

Try this if you want. I have a page I use to see what is being sent.
Change the url to this page and it will show you what is being sent.
For example if your header has "Content-Type: application/json" the POST data will not be in the form data it will be in the request body. that would be if the $postData were a json string. The data should be an array like you have, but the Content-Type: must be application/x-www-form-urlencoded
If you include another content type else in the $request it will be sent rather then the application/x-www-form-urlencoded .

You could post the contents of your $headers array.

Link to Get HTTP request headers and data

This is the code for receiveheaders.php

<?php
header('Content-Type: text/plain; charset=UTF-8');
foreach (getallheaders() as $name => $value) {
    echo "$name: $value\n";
}
echo "\nBODY\n";
echo file_get_contents('php://input');
echo "\n\$_POST\n";
var_export($_POST);
echo "\n\$_FILES\n";
var_export($_FILES);
echo "\n\$_SERVER\n";
var_export($_SERVER);

And if you click on it, the response will look similar to this.

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Cache-Control: no-cache
Connection: keep-alive
Dnt: 1
Host: eatled.com
Pragma: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0

BODY

$_POST
array (
)
$_FILES
array (
)
$_SERVER
array (
  'CONTEXT_DOCUMENT_ROOT' => '/home3/el/public_html',
  'CONTEXT_PREFIX' => '',
  'DOCUMENT_ROOT' => '/home3/el/public_html',
  'GATEWAY_INTERFACE' => 'CGI/1.1',
  'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
  'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
  'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5',
  'HTTP_CACHE_CONTROL' => 'no-cache',
  'HTTP_CONNECTION' => 'keep-alive',
  'HTTP_DNT' => '1',
  'HTTP_HOST' => 'eatled.com',
  'HTTP_PRAGMA' => 'no-cache',
  'HTTP_UPGRADE_INSECURE_REQUESTS' => '1',
  'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0',
  'PATH' => '/bin:/usr/bin',
  'QUERY_STRING' => '',
  'REDIRECT_STATUS' => '200',
  'REMOTE_ADDR' => '173.169.76.167',
  'REMOTE_PORT' => '61212',
  'REQUEST_METHOD' => 'GET',
  'REQUEST_SCHEME' => 'http',
  'REQUEST_URI' => '/receiveheader.php',
  'SCRIPT_FILENAME' => '/home3/el/public_html/receiveheader.php',
  'SCRIPT_NAME' => '/receiveheader.php',
  'SCRIPT_URI' => 'http://eatled.com/receiveheader.php',
  'SCRIPT_URL' => '/receiveheader.php',
  'SERVER_ADDR' => '23.111.132.114',
  'SERVER_ADMIN' => 'webmaster@eatled.com',
  'SERVER_NAME' => 'eatled.com',
  'SERVER_PORT' => '80',
  'SERVER_PROTOCOL' => 'HTTP/1.1',
  'SERVER_SIGNATURE' => '',
  'SERVER_SOFTWARE' => 'Apache',
  'TZ' => 'America/New_York',
  'UNIQUE_ID' => 'Yx1X9nNaf-LlLkU6uxWSgAAAABQ',
  'PHP_SELF' => '/receiveheader.php',
  'REQUEST_TIME_FLOAT' => 1662867446.909621,
  'REQUEST_TIME' => 1662867446,
  'argv' => 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM