我需要一些帮助来解析下面的 php 代码。不知道我错过了什么。我试图通过 POST 调用 API,但我得到的响应是凭据无效。我在邮递员中尝试过同样的事情,得到正确的响应。但使用下面的代码,我具有相同的凭据,我得到的响应是无效凭据。
<?php
session_start();
error_reporting(E_ALL); // Set to show all errors for debugging
// Database connection (using PDO for this example)
// Fetch API username and password from the apiusers table
try {
$sql = 'SELECT username, password FROM apiusers WHERE BPD_ID = :BPD_ID';
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':BPD_ID', $BPD_ID);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
$apiUsername = $user['username'];
$apiPassword = $user['password'];
// Display username and password in console
echo "<script>console.log('Username: " . addslashes($apiUsername) . "');</script>";
echo "<script>console.log('Password: " . addslashes($apiPassword) . "');</script>";
} catch (Exception $e) {
die('Failed to fetch API user data: ' . $e->getMessage());
}
//从数据库中获取数据
// Build the postData array
$postData = json_encode([
"supplyType" => "O",
"subSupplyType" => "8",
"subSupplyDesc" => "DC",
.........
"vehicleType" => "R",
"itemList" => $itemData
]);
// Output the request data to the console
echo "<pre>";
print_r($postData);
echo "</pre>";
echo "<pre>";
print_r($itemData);
echo "</pre>";
完整请求:
{
"supplyType": "O",
"subSupplyType": "8",
"subSupplyDesc": "DELIVERY CHALLAN",
"docType": "CHL",
"docNo": "DC242501",
"docDate": "01/08/2024",
"fromGstin": "29AAFCC9980M1ZR",
"fromTrdName": "welton",
"fromAddr1": "2ND CROSS NO 59 19 A",
"fromAddr2": "GROUND FLOOR OSBORNE ROAD",
"fromPlace": "FRAZER TOWN",
"fromPincode": 560090,
"actFromStateCode": 29,
"fromStateCode": 29,
"toGstin": "29AEKPV7203E1Z9",
"toTrdName": "sthuthya",
"toAddr1": "Shree Nilaya",
"toAddr2": "Dasarahosahalli",
"toPlace": "Beml Nagar",
"toPincode": 560090,
"actToStateCode": 29,
"toStateCode": 29,
"transactionType": 1,
"otherValue": "0",
"totalValue": 40000,
"cgstValue": 0,
"sgstValue": 0,
"igstValue": 0,
"cessValue": 0,
"cessNonAdvolValue": 0,
"totInvValue": 47200,
"transporterId": "",
"transporterName": "",
"transDocNo": "",
"transMode": "1",
"transDistance": "100",
"transDocDate": "",
"vehicleNo": "PVC1234",
"vehicleType": "R",
"itemList": [
{
"productName": "MS & SCRAP",
"productDesc": "SCRAP",
"hsnCode": 7402,
"quantity": 4,
"qtyUnit": "BOX",
"cgstRate": 9,
"sgstRate": 9,
"igstRate": 0,
"cessRate": 0,
"cessNonadvol": 0,
"taxableAmount": 40000
}
]
}
// cURL 设置
$url = 'https://my.gstzen.in/~gstzen/a/ewbapi/generate/?username=' . rawurlencode($apiUsername) . '&password=' . rawurlencode($apiPassword);
$apiKey = 'de3a3a01-273a-4a81-8b75-13fe37f14d';
$gstin = '29AAFCC9980M1'; // Your GSTIN value
/* $gstin = $mainData['COMP_GSTIN']; // Your GSTIN value */
// Initialize cURL session
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: API key ' . $apiKey,
'gstin: ' . $gstin
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// Execute cURL request and get the response
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
exit;
}
curl_close($ch);
// Decode the JSON response
$responseData = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
die('JSON decode error: ' . json_last_error_msg());
}
// Debugging the API response
echo "<script>console.log('API Response: " . addslashes(json_encode($responseData)) . "');</script>";
// Check for authentication errors
if (isset($responseData['status']) && $responseData['status'] === 'error' && isset($responseData['message'])) {
die('API error: ' . $responseData['message']);
}
// Insert data into the database
$pdo->commit();
echo 'Data inserted successfully';
} catch (Exception $e) {
$pdo->rollBack();
echo 'Failed to insert data: ' . $e->getMessage();
}
?>
API 调用的响应:
console.log('API Response: {\\"status\\":0,\\"message\\":\\"The user credentials provided in the request are invalid.\\"}');
邮递员回复附为图片
向 Postman 请求 JSON
{
"supplyType": "O",
"subSupplyType": "8",
"subSupplyDesc": "DELIVERY CHALLAN",
"docType": "CHL",
"docNo": "DC242501",
"docDate": "01/08/2024",
"fromGstin": "29AAFCC9980M1ZR",
"fromTrdName": "welton",
"fromAddr1": "2ND CROSS NO 59 19 A",
"fromAddr2": "GROUND FLOOR OSBORNE ROAD",
"fromPlace": "FRAZER TOWN",
"fromPincode": 560090,
"actFromStateCode": 29,
"fromStateCode": 29,
"toGstin": "29AEKPV7203E1Z9",
"toTrdName": "sthuthya",
"toAddr1": "Shree Nilaya",
"toAddr2": "Dasarahosahalli",
"toPlace": "Beml Nagar",
"toPincode": 560090,
"actToStateCode": 29,
"toStateCode": 29,
"transactionType": 1,
"otherValue": "0",
"totalValue": 40000,
"cgstValue": 0,
"sgstValue": 0,
"igstValue": 0,
"cessValue": 0,
"cessNonAdvolValue": 0,
"totInvValue": 47200,
"transporterId": "",
"transporterName": "",
"transDocNo": "",
"transMode": "1",
"transDistance": "100",
"transDocDate": "",
"vehicleNo": "PVC1234",
"vehicleType": "R",
"itemList": [
{
"productName": "MS & SCRAP",
"productDesc": "SCRAP",
"hsnCode": 7402,
"quantity": 4,
"qtyUnit": "BOX",
"cgstRate": 9,
"sgstRate": 9,
"igstRate": 0,
"cessRate": 0,
"cessNonadvol": 0,
"taxableAmount": 40000
}
]
}
来自 Postman 的响应 JSON
{
"status": 1,
"uuid": "cc1e4f5d-43ca-4373-9fd4-6e059fb6716a",
"message": "EWB Generated",
"alert": "",
"validUpto": "06/08/2024 11:59:00 PM",
"ewayBillNo": 171010516152,
"ewayBillDate": "05/08/2024 07:16:00 PM",
"EWayBillPdfUrl": "/~fnsjdjszvw/a/eway-bill/cc1e4f5d-43ca-4373-9fd4-6e059fb6716a/ewb/.pdf/",
"EWayBillQrCodeUrl": "/~fnsjdjszvw/a/ewaybill/cc1e4f5d-43ca-4373-9fd4-6e059fb6716a/qr-code/",
"Distance": "100"
}
邮递员的代码解决了问题。