why i cant get the json from this scrip in php using alamofire?

22 Views Asked by At

i wrote this script to sign in the user in PHP in order to save the data to the server, if the user has been created the scrip should return a simple json with true or false.

<?php

require "../private/autoload.php";
header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    print_r($_POST);
    $Error = "";

    $staff = $_POST['staff_ID'];
    $email = $_POST['email'];
    $pass = $_POST['password'];
    $name = $_POST['Name'];
    $cpt = $_POST['isCPT'];
    $date = date("Y-m-d H:i:s",time()); // date of creation
    // check if user alrady exixt
    $sqlexixst = "SELECT * FROM `users` WHERE staff_ID = ?";
    $st = $pdo->prepare($sqlexixst);
    $st->execute(array($staff));

    $result = $st->fetchAll();
    if (count($result) > 0){
    
        $array = array(
            "user_created"=>false
        );

        $js = json_encode($array);
        echo $js;

    } else {
        // user not exixt creo utente
        $regex = '/^[^0-9][_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
        if (preg_match($regex,$email)){
            
            // ok email

            if (is_numeric($staff)){
                // ok id

                $sql = "INSERT INTO users (staff_ID,password,email,isCPT,Name, date) VALUES (?,?,?,?,?,?)";

                $statement = $pdo->prepare($sql);

                $statement ->execute([$staff,$pass,$email,$cpt,$name,$date]);

                $array = array(
                 "user_created"=>true,
                 "staff_ID"=>$staff
                   );
                     $js = json_encode($array);
                     echo $js;


            }



        }else{
           
            $Error = "pls enter valid email";
            echo $Error;
        }
    }

   


}else {
    echo 'no post';
}



?>

i'm sending the request using Alamofire... if i post the request using respondeString i can see the corret print out of the json, if i use respondJSON i cant get the json print out.. i get error say 'JSON could not be serialized. thata could not be read because it isn't in the correct format'

Alamofire and swiftyjson code:

  func nxTest (){
        let parm : [String : Any] = ["staff_ID": "3879","password":"12345678","email":"[email protected]", "isCPT":false,"Name":"Marco Frizzi"]
        AF.request("http://192.168.50.10/nx/public/register.php", method: .post, parameters: parm,headers: nil, interceptor: nil, requestModifier: nil).validate()
        
            .responseJSON { js in
                switch js.result {
                case .success(let value) :
                    let json = JSON(value)
                    debugPrint(json)
                case .failure(let err) :
                    debugPrint(err.localizedDescription)
                }
                }
            
        .responseString { st in
            print(st)
        }
        }
0

There are 0 best solutions below