简体   繁体   中英

my PHP not return JSON

I try to learn basic PHP to connect between my server and android but I have a problem with my basic PHP it return html instead JSON.

this is my PHP code:

<?php
mysql_connect("sql100.0fees.us","*********","*******");
mysql_select_db("db shlomi");
$sql=mysql_query("select * from pru");
$output=array();
while($row=mysql_fetch_assoc($sql)) {
   $output[]= array(
       'name' => $row['name'],
     'quantity' => $row['quantity']);
}
echo(json_encode($output));
mysql_close();
?>

when I run this in my browser I receive [{"name":"10","quantity":"50"},{"name":"10","quantity":"50"}]

but in my app or in jsonlint.com I receive error like html

<html><body><scripttype="text/javascript"src="/aes.js"></script><script>functiontoNumbers(d){
    vare=[

    ];d.replace(/(..)/g,
    function(d){
        e.push(parseInt(d,
        16))
    });returne
}functiontoHex(){
    for(vard=[

    ],
    d=1==arguments.length&&arguments[
        0
    ].constructor==Array?arguments[
        0
    ]: arguments,
    e="",
    f=0;f<d.length;f++)e+=(16>d[
        f
    ]?"0": "")+d[
        f
    ].toString(16);returne.toLowerCase()
}vara=toNumbers("f655ba9d09a112d4968c63579db590b4"),
b=toNumbers("98344c2eee86c3994890592585b49f80"),
c=toNumbers("68c0fc9ff46cfabf092145e84011ee13");document.cookie="__test="+toHex(slowAES.decrypt(c,
2,
a,
b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";location.href="http://ranva.0fees.us/query.php?ckattempt=1";</script><noscript>ThissiterequiresJavascripttowork,
pleaseenableJavascriptinyourbrowseroruseabrowserwithJavascriptsupport</noscript></body></html>

so I understand that my problem is in the PHP because it didn't return JSON.

The response generated by your script is JSON but it is advertised as HTML in the header of the response because the default Content-Type returned by PHP scripts is text/html .

If your script generates anything else than HTML it has to mention this in the response by sending the correct Content-Type using the PHP function header() .

The correct content type for JSON content is application/json . Use the function header() to set the correct content type before sending any content to the browser.

header('Content-Type: application/json');
echo(json_encode($output));

Very important!

The mysql PHP extension is deprecated since PHP 5.5 and it was removed from PHP 7.
Forget it!

Learn and use either mysqli or PDO instead.

Your server is serving the HTML page to block "strange access" to your server. I read something about it in the following post:

https://wordpress.org/support/topic/it-looks-like-this-is-a-web-page-not-a-feed-i-looked-for-a-feed-associated

Maybe accessing from your app will be classified as "strange access".

Please contact your provider and ask them, if there is any protection guard (eg the "Spam shield function") enabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM