问题
Trying to get the user authentication work on Android.
The Android app sends username and password using POST method. But, I keep getting "Error receiving detail!!" error. I tried to use REST console to see if the web service works, but no success, I get the same error even there.
Any help or direction would be appreciated.
Have built a PHP web service and the code is:
<?php
require_once '../site_info.php';
require_once '../database_connect.php';
require_once '../functions.php';
if(isset($_REQUEST['email']) and $_REQUEST['email']!='' and isset($_REQUEST['password']) and $_REQUEST['password']!='')
{
$email = mysql_real_escape_string($_REQUEST['email']);
$pass = mysql_real_escape_string($_REQUEST['password']);
$query = 'select id from users where email = "'.$email.'" and password = "'.sha1($pass).'"';
$result=mysql_query($query) or die('error getting admin details : '.mysql_error());
if(mysql_num_rows($result)==1)
{
echo 'Login Success!!';
}
else
{
echo 'Incorrect login details!!';
}
}
else
{
echo 'Error recieving data!!';
}
?>
The Android app sends request as json:
JOSN request data:{"password":"password","email":"test@gmail.com"}
response
status:200
response body:array(0) {}Error recieving data!!
回答1:
else{
var_dump($_REQUEST); //add this line to see what's coming to your script
echo 'Error recieving data!!';
}
If you receive nothing then you can try using something to post on this script to check the script, but I guess the problem comes from the client.
In a command line (assuming you have cURL) :
curl --data "param1=value1¶m2=value2" http://example.com/script.php
回答2:
Found the solution
<?php
require_once '../site.php';
require_once '../database.php';
require_once '../functions.php';
$data = file_get_contents('php://input');
$json = json_decode($data);
$email = $json->{'email'};
$password = $json->{'password'};
if(!empty($email) and !empty($password))
{
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
$query = 'select id from users where email = "'.$email.'" and password = "'.sha1($password).'"';
$result=mysql_query($query) or die('error getting admin details : '.mysql_error());
if(mysql_num_rows($result)==1)
{
$array = array("success" => "TRUE");
print(json_encode($array));
}
else
{
$array = array("error" => "Login invalid.");
print( json_encode($array));
}
}
else
{
$array = array("error" => "Data not recieved.");
print( json_encode($array));
}
?>
来源:https://stackoverflow.com/questions/22147771/php-web-service-for-authenticating-user-login-on-android