Need help in debugging request and response in PHP calling an API

I need some help to resolve below php code. not sure what am i missing. I am trying to call an API through POST and in response i’m getting credentials are invalid. The same thing i tried in postman getting a response correctly. But with below code i with same credentials i’m getting a response as invalid credentials.

<?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());
}

// Fetch data from the database

// 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>";

Full Request:

{
    "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 setup

$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();
}
?>

response from API call:

console.log('API Response: {\"status\":0,\"message\":\"The user credentials provided in the request are invalid.\"}');

Postman response is attached as image

Request JSON from Postman

{
    "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
        }
    ]
}

Reponse JSON from Postman

{
    "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"
}

New contributor

Syed Mujahid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật