PHP file always gives false response

Viewed 116

I have an sms API which is called in a PHP file ,which is used in Android as well as in PWA application. This API was previously installed in a Linux based server on Apache and recently shifted to a windows based server to IIS. It is working fine in Linux based server but after shifting to windows based server , API is only giving false response even though all parameters has been provided. Any help would be really appreciated as I'm stuck in this from a week . In android studio I'm getting a 400 response when calling this API. im unable trace the issue.

<?php

ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 0);
ini_set('default_socket_timeout', 15);

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS');
    
    header('Content-Length: 0');
    header('Content-Type: text/plain');
    die();
}

header("Access-Control-Allow-Origin: *");
Cache-Control, Pragma, Accept, Accept-Encoding");
header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE");
header('Content-Type: application/json');

$params = json_decode(file_get_contents("php://input"), TRUE);
    if (empty($params)) {
        $params = $_POST;
    }

$invalid_params = (empty($params) || empty($params['username']) || empty($params['password'] || $params['senderid']) || empty($params['message']) || empty($params['mobilenumber']));

if ($invalid_params) {
    http_response_code(200);
    echo json_encode(['status' => false, 'msg' => 'Invalid Parameters']);
    exit;
} else {
    try {
        $params = json_decode(file_get_contents("php://input"), TRUE);
        if (empty($params)) {
            $params = $_POST;
        }
        $url = "http://ip/smsapi/httpapi.jsp?username=" . urlencode($params['username']) . "&password=" . $params['password'] . "&from=" . urlencode($params['senderid']) . "&to=" . urlencode($params['mobilenumber']) . "&text=" . urlencode($params['message']) . "&coding=0&flash=0";
        // echo $url;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        //echo $code;
        curl_close($ch);
        if ($code == 200) {
            $xml = simplexml_load_string($result);
            $json = json_encode($xml);
            $arr = json_decode($json, true);
            if (!empty($arr['ack_id'])) {
                response_handler();
            } else {
                error_handler();
            }
        } else {
            error_handler();
        }
    } catch (Exception $e) {
        error_handler();
    }
}

function error_handler()
{
    http_response_code(200);
    echo json_encode(['status' => false, 'msg' => 'Failed to send otp']);
    exit;
}

function response_handler()
{
    http_response_code(200);
    echo json_encode(['status' => true, 'msg' => 'Otp Send Successfully']);
    exit;
}

0 Answers
Related