0

I am practicing PHP and sending request on a server using REST Client add-on in Mozilla Firefox. I also tried different tool on chrome to check whether i am receiving data on server but unfortunately i am not receiving anything neither GET or POST. Only thing which i am able to receive is query string.

I didn't even write any code on server side just simple print commands.

<?php

var_dump($_REQUEST);
var_dump($_GET);
var_dump($_POST);
print_r($_GET);
print_r($_POST);

echo "working";


?>

This is INPUT

> This is test message.

And this is output.

array(0) {
}
array(0) {
}
array(0) {
}
Array
(
)
Array
(
)
working

Like is said only thing that works is query string. And this server i am using is free web service don't know if this helps.

This is android code that i am trying to receive.

byte[] jsonBytes = json.getBytes("UTF-8");

            URL url = new URL("http://androidapplicati.base.pk/");
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
             urlConnection.setDoOutput(true);
            urlConnection.setFixedLengthStreamingMode(jsonBytes.length);
             urlConnection
             .setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestProperty("Content-Length",
                    String.valueOf(jsonBytes.length));

             out = urlConnection.getOutputStream();
             out.write(jsonBytes);
             out.flush();
             urlConnection.getInputStream().read();
Charles
  • 50,943
  • 13
  • 104
  • 142
Shah
  • 73
  • 1
  • 1
  • 9

2 Answers2

0

First of all please check did you pass any value using any method GET or POST. if yes than This is not your coding problem . This is your hosting server issue. my advice never use any free servers because there is not technical support and they are not secure..

Your Code it working OK just change hosting server

0

Here is a POST example, you can test get by directly visiting or file_get_contents().

Post(cURL):

//domain and file to send request to
$url = 'http://yourdomain.com/file.php';

//parameters to send
$params = array(
          'field1' => 'value1',
          'field2' => 'value2'
        );

$data = http_build_query($params);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
//return the data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//incase there is a redirect
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$return = curl_exec($ch);

//close connection
curl_close($ch);

$output = json_decode($return, TRUE);

var_dump($output);

And on server side:

echo json_encode($_REQUEST);

Niket Malik
  • 1,075
  • 1
  • 14
  • 23