1

I am trying to use a PHP CURL request to upload data to Pass Slot to change an image, and I am continually getting errors.

This is the CURL request needed from their developer section on their website

POST https://api.passslot.com/v1/passes/pass.example.id1/27f145d2-5713-4a8d-af64-b269f95ade3b/images/thumbnail/normal

and this is the data that needs to be sent in its requested format

------------------------------330184f75e21
Content-Disposition: form-data; name="image"; filename="icon.png"
Content-Type: application/octet-stream
 
.PNG
imagedata

This is the code I am using currently, as I am not familiar with what is required on Multipart Form requests on API

$passId = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx";
$pass_generate_url = "pass.xxxxxxxxxxxx";
$url1 = 'https://api.passslot.com/v1/passes/'.$pass_generate_url.'/'.$passId.'/images/strip/normal';

$logo_file_location = "image.png";
$logo_file_location1 = "http://xxxxxxx.com/uploads/";

$data1 = array('image' => '@uploads/'.$logo_file_location,'application/octet-string',$logo_file_location1,'some_other_field' => 'abc',);

$auth1 = array(  'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=',
'Content-Type: text/plain');

$ch1 = curl_init($url1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data1);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $auth1);

$response1 = curl_exec($ch1);

$ch1 = curl_init($url1);

When I run the code, this is the response from the CURL I get

{"message":"Validation Failed","errors":[{"field":"image","reasons":["Required"]}]}

Is there something I need to add to make the code work please?

Renegade Rob
  • 393
  • 1
  • 4
  • 21
  • @KenLee I haven't. Not even sure what to put in to be honest. Never done a multipart form API – Renegade Rob Oct 13 '22 at 00:32
  • should not you set auth1 header prior to postfields? – Zubair Ahmd Oct 13 '22 at 00:41
  • @ZubairAhmd I dont think the order is important as the curl_setopt builds the curl irrespective of the order its placed, then the curl_exec sends it off. – Renegade Rob Oct 13 '22 at 00:44
  • @RenegadeRob got it. then its probably some thing wrong with it $auth1 as message says validation failed or may be image field path is incorrect and failing the validation – Zubair Ahmd Oct 13 '22 at 00:53

1 Answers1

1

Yes, I think you can build your own curl including to use say PHP CURLFile (sending the Multipart delimiter boundary and then the graphic data, etc) But you may choose to use an API (Say PassSlot PHP SDK)

https://github.com/passslot/passslot-php-sdk

General usage

require 'passslot-php-sdk/src/PassSlot.php';

$engine = PassSlot::start('<YOUR APP KEY>');
$pass = $engine->createPassFromTemplate(<Template ID>);
$engine->redirectToPass($pass);

For PNG file, it is like:

<?php
require_once('../src/PassSlot.php');

$appKey ='<YOUR APP KEY>';
$passTemplateId = 123456;
$outputPass = FALSE;

$values = array(
    'Name' => 'John',
    'Level' => 'Platinum',
    'Balance' => 20.50
);

$images = array(
    'thumbnail' => dirname(__FILE__) . '/thumbnail.png'
);

try {
    $engine = PassSlot::start($appKey);
    $pass = $engine->createPassFromTemplate($passTemplateId, $values, $images);

    if($outputPass) {
        $passData = $engine->downloadPass($pass);
        $engine->outputPass($passData);
    } else {
        $engine->redirectToPass($pass);
    }
} catch (PassSlotApiException $e) {
    echo "Something went wrong:\n";
    echo $e;
}

For further reference, please visit this

https://github.com/passslot/passslot-php-sdk/blob/master/examples/example.php

You may also view the source of the API to get inspired:

https://github.com/passslot/passslot-php-sdk/blob/master/src/PassSlot.php

Additional remark:

In case there is certificate expiry warning/error when running the SDK, please download the latest cacert.pem from https://curl.se/docs/caextract.html and replace the one in the SDK

Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • Using the sdk you mentioned gives me this error though. I am looking into why, but cant find it. "Something went wrong first section: [60]: Please check your PHP / libcurl version and ensure that the latest security protocols are supported (The certificate issuer's certificate has expired. Check your system date and time.)" – Renegade Rob Oct 13 '22 at 01:47
  • 1
    (1) This can be due to the cacert being outdated, please visit the following [SO link](https://stackoverflow.com/questions/23032165/how-do-i-keep-my-cacert-pem-current-for-security-reasons-when-using-curl) and see their suggestions (2) Alternatively, use [this](https://curl.se/docs/caextract.html) to replace the file in the SDK – Ken Lee Oct 13 '22 at 01:51
  • Hi Ken, you are a legend, the file to replace in the SDK worked a treat. Thank you. From the bottom of my heart, thank you. – Renegade Rob Oct 13 '22 at 02:15