I am new to PHP. I have an android app that sends JSON string to myphp.php
but it gets redirected to index.php
. Is this normal behavior? How can I go around that?
Code
URL url= null;
HttpURLConnection urlConn;
url = new URL ("http://192.***.*.**/myserver/myphp.php");
urlConn = (HttpURLConnection)updateurl.openConnection();
This is the content of myphp.php.
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//include '/index.php';
$json = file_get_contents("php://input");
$decoded = json_decode($json, TRUE);
$lat = $decoded['lat'];
$lon = $decoded['lon'];
sendID($pasid, $lat, $lon);
function sendID($tokenid, $lat, $lon) {
//request url
$url = 'https://android.googleapis.com/gcm/send';
//your api key
$apiKey = 'key';
//payload data
$data = array('lat' => $lat, 'lon' => $lon);
//$registration_ids = array($tokenid);
$message_Id = "0000001";
$fields = array('to' => $tokenid, 'data' => $data);
//http header
$headers = array('Authorization: key=' . $apiKey,
'Content-Type: application/json');
//curl connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_error($ch); //see the error details
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}