简体   繁体   中英

How do I post JSON payloads to a web service using PHP?

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\": \"mia@myisp.com\"";
        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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM