Home > other >  Multipart Form CURL request in PHP uploading image
Multipart Form CURL request in PHP uploading image

Time:10-13

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?

CodePudding user response:

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

  • Related