【发布时间】:2018-05-09 12:24:09
【问题描述】:
我正在尝试在我的网站上使用 Xsolla 支付系统...他们有一个 PHP SDK,但我在使用它时遇到了问题 - 所以我通常使用 cURL 来使用它。
Xsolla 文档中的 cURL 命令是:
curl -v https://api.xsolla.com/merchant/merchants/{merchant_id}/token \
-X POST \
-u your_merchant_id:merchant_api_key \
-H 'Content-Type:application/json' \
-H 'Accept: application/json' \
-d '
{
"user": {
"id": {
"value": "1234567"
},
},
"settings": {
"project_id": 14004
},
"purchase": {
"virtual_items": {
"items": {
"sku": "item"
},
},
}
}'
我是这样把它转换成 PHP 的:
<?php
$ch = curl_init();
$data = array(
"user" => array(
'id' => array('value' => '1234567')
),
"settings" => array(
'project_id' => 29039
),
"purchase" => array(
'virtual_items' => array(
'items' => array(
'sku' => '1'
)
)
)
);
curl_setopt($ch, CURLOPT_URL, "https://api.xsolla.com/merchant/merchants/{My merchant id}/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "{My merchant id}" . ":" . "{My api key}");
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
echo $result;
curl_close ($ch);
?>
但是我得到:{ "http_status_code": 422, "message": "The parameter settings.project_id is required.", "extended_message": { "global_errors": [], "property_errors": [] }, "request_id": "bfd6f72" }
我确定问题出在$data,但我不知道如何解决。
任何帮助将不胜感激。
使用@Magnus Eriksson 的解决方案后更新(仍未解决):
$data = array(
"user" => array(
'id' => array('value' => '1234567')
),
"settings" => array(
'project_id' => 29039
),
"purchase" => array(
'virtual_items' => array(
'items' => array(
'sku' => '1'
)
)
)
);
$data_json = json_encode($data);
现在我使用 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); 代替 $data。
我现在得到的错误是:{ "http_status_code": 422, "message": "JSON is not valid against json schema, please check documentation http:\/\/developers.xsolla.com\/api.html#payment-ui", "extended_message": { "global_errors": [], "property_errors": { "purchase.virtual_items.items": [ "Object value found, but an array is required" ] } }, "request_id": "34dc63e" }
谢谢!
【问题讨论】:
-
服务似乎需要 JSON,而不是普通的 POST 字段。
-
我尝试添加
$data_json = json_encode($data);并在 POSTFIELDS 中使用编码的 JSON 而不是数组,但我得到另一个错误。新的错误是JSON is not valid against json schema@MagnusEriksson -
已更新为新代码。 @MagnusEriksson
-
所以文档错误?