David
David

Reputation: 39

Json data from php API at online hosting

I need your advice. I made API in php to communicate with my android application and mySQL database. Now I wanna put this api on free online hosting with free database, the problem is that when i make query for that API I'm receiving my json data with junk from hosting like HTML tags and commercial text "[hosting name] free hosting". When my app receives this data, it shuts down. Everything works fine on local wamp server but at online hosting my app is crashing

I have 3 questions for you

  1. Is it normal on free hosting or maybe my API is wrong designed?
  2. If I put my php files on paid serwer will I avoid this additional stuff added by hosting company?.
  3. Check out part of my sample user registration php code

            $new_sql_select_query = "select * from userinfo where userName like '$userName' and userEmail like '$userEmail';";
    
            $sql_all_data = mysqli_query($con, $new_sql_select_query);
    
            $userDataJson = array();
    
            while ($row = mysqli_fetch_array($sql_all_data)) {
            $userDataJson["userId"] = $row["userId"];
            $userDataJson["userName"] = $row["userName"];
            $userDataJson["userEmail"] = $row["userEmail"];
            $userDataJson["accountBalance"] = $row["accountBalance"];
        }
    
    
        $responseJson["success"] = 1;
        $responseJson["message"] = "User correctly added to base!";
        array_push($responseJson["user"], $userDataJson);
        echo json_encode($responseJson);
    

I have an idea but I do not know how to do it correctly. I am generating a new json data file by code below

        $myjson = json_encode($responseJson);
        file_put_contents('myfile.json', $myjson);

but here is another problem, my app need to be somehow redirected to this new file because right now my app is connecting directly to a specific php file in this case CreateNewUserDB.php so how should I do it?. Should I return link to this generated json file to my app and then make another connection but this time to this "myfile.json" file?. Waiting for answers. Regards

Upvotes: 0

Views: 1705

Answers (3)

grc
grc

Reputation: 324

This is an old thread, but I had a similar problem recently.

I uploaded my php json api in my shared hosting and solved the problem setting the right format by adding the header for json in the php file:

header('Content-Type: application/json');

Upvotes: 1

Nick Asher
Nick Asher

Reputation: 756

The reason that your app might be crashing is that when you do send response to your app on localhost, then only json data is sent. But as you said on the free hosting, you got some html. When your java code tried to make a json object out of it, it must have thrown an exception and hence the error.

There are plenty of free hosting, that are good and don't do these type of advertisements. https://www.biz.nf/ is one of them. I used it in my early years. Also paid hosting will not give you problems like these

Upvotes: 1

Galalen
Galalen

Reputation: 64

I encourage you to work with Firebase, it will handle all the background staff for you, and it gives you access to the database also, besides that, it's very fast comparing to regular databases.

Upvotes: 0

Related Questions