我试图使用PHP CURL请求上传数据以传递插槽以更改图像,而且我不断地收到错误。
这是他们的网站上的开发者部分所需要的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"]}]}有什么我需要添加的东西使代码工作吗?
发布于 2022-10-13 01:16:54
是的,我认为您可以构建自己的curl,包括使用CURLFile (发送多部分分隔符边界,然后发送图形数据等),但是您可以选择使用API (比如PassSlot PHP )。
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时出现证书过期警告/错误,请从cacert.pem下载最新的https://curl.se/docs/caextract.html并替换SDK中的
https://stackoverflow.com/questions/74048931
复制相似问题