Selly.gg API集成报错:CURL POSTFIELDS参数验证失败
我来帮你捋捋这个问题哈,从你收到的一堆错误提示和代码来看,核心问题是请求格式和参数传递的方式不匹配,同时部分参数的有效性也需要校验,咱们一步步来解决:
问题根源分析
你收到的所有错误(比如字段为空、格式不对、网关不支持),本质是因为你的请求体格式和API要求的不匹配,导致API无法正确解析参数,进而误判所有字段都有问题。
具体修正步骤
1. 调整Content-Type与请求体格式
你现在代码里设置的是application/x-www-form-urlencoded,但却用json_encode($data)把参数转成了JSON字符串,这就完全矛盾了——API以为你要传表单格式,结果收到的是JSON,自然解析失败。
解决方法分两种,选一种就行:
方法一:用JSON格式请求(推荐,大部分现代API优先支持)
把Content-Type改成application/json,保持json_encode($data)的处理方式:
// ... 其他代码不变 ... $headers = array(); $headers[] = "Authorization: Basic ".base64_encode('EMAIL:APIKEY'); $headers[] = "Content-Type: application/json"; // 这里修改为JSON类型 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // ... 其他代码不变 ...
方法二:用表单格式请求
如果想用application/x-www-form-urlencoded,就把json_encode($data)换成http_build_query($data),这样参数会被转成标准的表单格式:
// ... 其他代码不变 ... $data_curl = http_build_query($data); // 替换json_encode // ... 其他代码不变 ... $headers[] = "Content-Type: application/x-www-form-urlencoded"; // 保持这个 // ... 其他代码不变 ...
2. 确保参数的有效性
错误里提到的Email can't be blank、Value is not a number,你需要先校验这些变量的合法性:
- 检查
$_GET["email"]是否存在且不为空:$email = isset($_GET["email"]) ? trim($_GET["email"]) : ''; if (empty($email)) { die("Email is required"); } - 确保
$price和$_GET["stock"]都是数字,避免计算出非数字值:$stock = isset($_GET["stock"]) ? intval($_GET["stock"]) : 0; $price = isset($price) ? floatval($price) : 0; $totalValue = $price * $stock; if ($totalValue <= 0) { die("Invalid total payment value"); }
3. 确认网关参数的正确性
错误里说Gateway is not included in the list,你需要对照官方文档确认Selly.gg支持的网关值,比如PayPal是否需要小写(paypal),或者有没有其他拼写要求,别因为大小写或者拼写错误导致不识别。
修正后的完整代码示例(JSON格式)
$ch = curl_init(); // 参数校验与处理 $email = isset($_GET["email"]) ? trim($_GET["email"]) : ''; $stock = isset($_GET["stock"]) ? intval($_GET["stock"]) : 0; $price = isset($price) ? floatval($price) : 0; $totalValue = $price * $stock; // 校验必填参数 if (empty($email) || $totalValue <= 0) { die("Missing or invalid required parameters"); } $data = array( "title" => "My Payment", "gateway" => "paypal", // 确认正确的网关值 "email" => $email, "value" => $totalValue, "currency" => "USD", "return_url" => "http://example.com", "webhook_url" => "http://example.com/callback.php?paid=true" ); $data_curl = json_encode($data); curl_setopt($ch, CURLOPT_URL, "https://selly.gg/api/v2/pay"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_curl); curl_setopt($ch, CURLOPT_POST, 1); $headers = array(); $headers[] = "Authorization: Basic ".base64_encode('EMAIL:APIKEY'); // 替换成你的实际邮箱和API密钥 $headers[] = "Content-Type: application/json"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close ($ch); echo $result;
额外建议
- 测试时可以先用固定值代替
$_GET参数(比如直接写"email" => "test@example.com"),排除GET参数为空的问题; - 打印
$data数组,确认所有字段都有正确的值; - 确认你的API密钥和邮箱的base64编码是否正确,比如
base64_encode('your-email@example.com:your-api-key'),不要有多余的空格。
内容的提问来源于stack exchange,提问作者Mario
相关产品推荐
相关产品推荐

