views:

56

answers:

1

Hi all, we are using a web service which receives JSON payloads and sends back JSON payloads as response. I know how to do it c#, but unfortunately it doesn't seem easy doing the same thing in PHP. here is our c# code, what is the equivalent piece of PHP code of this??

        String str = "https://thewebservice.com";
        str += "?schema=1.0";
        str += "&form=json";
        str += "&token=securedtoken";
        str += "&account=38939";
        HttpWebRequest req = (HttpWebRequest) WebRequest.Create(str);
        req.Method = "POST";

        string strRequest = "";
        strRequest += "{";
        strRequest += "\"$xml\": {";
        strRequest += "\"plu$user\": \"http://xml.type.com/User\"";
        strRequest += "},";
        strRequest += "\"plu$userName\": \"Mia\",";
        strRequest += "\"plu$password\": \"secret\",";
        strRequest += "\"plu$fullName\": \"Mia Jones\",";
        strRequest += "\"plu$email\": \"[email protected]\"";
        strRequest += "}";
        req.ContentLength = strRequest.Length;
        StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
        streamOut.Write(strRequest);
        streamOut.Close();
        StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
        while (!streamIn.EndOfStream)
            Response.Write(streamIn.ReadToEnd());
        streamIn.Close();
A: 

Look at using the json_encode function. It will take an array and generate a JSON string.

Bob Baddeley