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

时间:2010-08-10 04:32:16

标签: php web-services json

我们正在使用一个Web服务,它接收JSON有效负载并将JSON有效负载作为响应发回。我知道怎么做c#,但遗憾的是在PHP中做同样的事情似乎并不容易。这是我们的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();

1 个答案:

答案 0 :(得分:0)

使用json_encode功能查看。它将采用一个数组并生成一个JSON字符串。