I have created this code to fetch the data from android app. after fetching the json data i am decode them and then i fetch the key variable that is base 64 code of image and ori_name variable which is image name. after that i am creating image file at server side from base 64 code and store the file in server. if image is store in server then iam storing the image name and image path in one database. when i am executing this code with android app it only return nil at android side. is here any solution for fetching data? and note the android app is developed by my friend who use my created this service. he getting the nil value when he use it. please help me to solve this issue.

<?php
//process client request(via URL)
header("content-Type:application/json");
$data=$_REQUEST['jdata'];//request json data
$imgdata=json_decode($data);//decode json data
$basecode=$imgdata->{'key'};//fetch base64 code of image
$imgname=$imgdata->{'ori_name'};//fetch image name
$response=get_url($basecode,$imgname);//cal function for store image at server
return deliver_response($response);
function deliver_response($status)
{
header("HTTP/1.1");
$response['status']=$status;
$json_response=json_encode($response);
return $json_response;//return json response to android app
}
function get_url($basecode,$imgnm)
{
$decoded= base64_decode($basecode);//convert base 64 code to string format

$location=$_SERVER['HTTP_HOST']."/Clients/krishinternational/MobileService/images/".$imgnm;
$output_file="../images/".$imgnm;//define output file location
$ifp = fopen($output_file, "wb"); //create file
    $rs=fwrite($ifp, $decoded); //write image in file
    if($rs!=False)//check weather file is written or not
    {
        fclose($ifp); 
        $conn =mysqli_connect('host','user','password','db_name');//creating database connection
        if ($conn->connect_error) 
        {
        die("Connection failed: " . $conn->connect_error);
        } 


        $query="insert into tblimage values('".$imgnm."','".$location."')";//query to store image path in database
        if ($conn->query($query) === TRUE) 
        {
        //echo "New record created successfully";                               
        mysqli_close($conn);
        return True;//if successfully inserted then return true
        }           
        else 
        {
        return False;//if not succeed then return false
        }
      }
        else
       {
        return False;
       }
    }
    ?>
1

There are 1 answers

0
Safi Belim On

Thanks Indra I got the result testing by Rest Client. the error was i am requesting the json data from android app but the actually i have to use file_get_contents.

$input=file_get_contents("php://input");

thanks indra