简体   繁体   English

如何使用PHP将JSON有效负载发布到Web服务?

[英]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. 我们正在使用一个Web服务,该服务接收JSON有效负载并发送回JSON有效负载作为响应。 I know how to do it c#, but unfortunately it doesn't seem easy doing the same thing in PHP. 我知道如何用c#做到这一点,但是不幸的是,在PHP中做同样的事情似乎并不容易。 here is our c# code, what is the equivalent piece of PHP code of this?? 这是我们的C#代码,这等价的PHP代码是什么?

        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. 查看使用json_encode函数。 It will take an array and generate a JSON string. 它将使用一个数组并生成一个JSON字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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