2

My purpose is to send JSON from C# to PHP and then further decode it on PHP side to use it for my purpose.
Error at PHP end is:

Warning: json_decode() expects parameter 1 to be string, array given in /home3/alsonsrnd/public_html/test/sync.php on line 24

I am using the following code to send JSON to my PHP code using C#:

  private void sendData()
     {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://alnnovative.com/test/sync.php");
        httpWebRequest.ContentType = "text/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"user\":\"test\"," +
                          "\"password\":\"bla\"}";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }
    }

On PHP I am using strip slashes and json_decode() in order to decode JSON. My PHP code gives error that the received string is an array instead of JSON and thus it cannot use json_decode to decode it. What could be the reason of it? As I am sending the correct format of JSON from C#

My PHP code is:

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $json=$_POST;
    if (get_magic_quotes_gpc()){
        $json = stripslashes($json);
    }

    //Decode JSON into an Array
    $data = json_decode($json);
}
23
  • It is present in the code. string json=..... Commented Oct 29, 2015 at 9:57
  • Is it a typo - screencast.com/t/7bc2MCQpzko4? Seems names of variables not match. Commented Oct 29, 2015 at 9:57
  • Can you add a debug line on your PHP server to print out the JSON it receives? (Before and after the slash stripping) Commented Oct 29, 2015 at 9:58
  • Can we see the PHP code? Commented Oct 29, 2015 at 9:58
  • 2
    Look at the first answer here - stackoverflow.com/questions/13000386/… Commented Oct 29, 2015 at 10:50

1 Answer 1

0

Firstly, you have to change your Content-Type header

httpWebRequest.ContentType = "application/x-www-form-urlencoded";

Secondly, you are sending data in wrong format

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "json=" + "{\"user\":\"test\"," +
                              "\"password\":\"bla\"}"; ;

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

And finally, you are receiving data in wrong way, $_POST is an array, so

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['json']))
{
    $json=$_POST['json'];
    if (get_magic_quotes_gpc()){
        $json = stripslashes($json);
    }
    //Decode JSON into an Array
    $data = json_decode($json, true);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.