我正在嘗試使用 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 上的 Multipart Form 請求所需的內容
$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"]}]}
請問我需要添加什么來使代碼正常作業嗎?
uj5u.com熱心網友回復:
是的,我認為您可以構建自己的 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 中的
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/515975.html
標籤:php形式api卷曲
上一篇:提交后如何呈現表單?
