PHP中的多部分表单CURL请求上传图像

qyyhg6bp  于 2022-11-13  发布在  PHP
关注(0)|答案(1)|浏览(133)

我尝试使用PHP CURL请求将数据上传到Pass Slot以更改图像,但我不断收到错误。
这是他们网站上的开发人员部分需要的CURL请求

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

并且这是需要以其请求的格式发送的数据

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

这是我目前正在使用的代码,因为我不熟悉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);

当我运行代码时,这是我从CURL得到的响应

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

有什么我需要添加,使代码的工作请?

xytpbqjk

xytpbqjk1#

是的,我认为您可以构建自己的curl,包括使用PHP CURLFile(发送多部分分隔符边界,然后发送图形数据等),但您可以选择使用API(例如PassSlot PHP SDK)
https://github.com/passslot/passslot-php-sdk
一般用途

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

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

对于PNG文件,如下所示:

<?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;
}

如需进一步参考,请访问
https://github.com/passslot/passslot-php-sdk/blob/master/examples/example.php
您也可以查看API的源代码以获得启发:
https://github.com/passslot/passslot-php-sdk/blob/master/src/PassSlot.php

附加备注:

如果在运行SDK时出现证书过期警告/错误,请从https://curl.se/docs/caextract.html下载最新的cacert.pem,并替换SDK中的cacert. pem

相关问题