views:

109

answers:

1

hi all,

we're using file_get_contents to communicate with a web service, which creates users and if succeeds it returns a JSON object with the details of the new created user. the code below shows how we do it, the user is successfully created, meaning we can see it from the back-end, however, we just can't get the JSON response, it returns nothing.

public function register(){
    $username = "testing";
    $email = "[email protected]";
    $password = "testpsd";

    $userData = '{"$xmlns": {"pluser": "http://xml.webservice.com/auth/data/User"},'
            .'"pluser$userName": "'.$username.'",'
            .'"pluser$password": "'.$password.'",'
            .'"pluser$fullName": "fullname",'
            .'"pluser$email": "'.$email.'"}';
    $url = 'https://webservice.com?form=json';
    $cparams = array('http' => array('method' => 'POST','ignore_errors' => true));
    $cparams['http']['content'] = $userData;      
    $cparams['http']['request_fulluri'] = true;
    $cparams['http']['header'] = 'Content-type: application/json';
    $context = stream_context_create($cparams);

    $fp = @file_get_contents($url,false,$context);$res = stream_get_contents($fp);
    print_r($res);
}

at first we thought the web service was supposed to return nothing, so we tested against it in c# which worked perfectly fine, meaning we got the create response of something like {"stutas":"successful","userCreated":"true"} here is the c# code:

String url = "https://webservice.com?form=json";
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
        req.Method = "POST";

        string strRequest = "exactly the same json string";
        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)
            Console.WriteLine(streamIn.ReadToEnd());
        streamIn.Close();

        Console.ReadKey();}

is there anything missing or misconfigured in the php code?

A: 

The PHP function file_get_contents will get the entire contents of the response. You don't need the $res = stream_get_contents($fp). The response will already be in $fp.

You can just do this:

$fp = @file_get_contents($url,false,$context);
print_r($fp);
Dumb Guy
great!Thanks a lot!I've got the response however it's a java exception.why it returns an exception given that the url and data passed in are exactly the same?why this works perfectly fine in c#?
walter
That probably has to do with your web back-end. You could try removing the content-type header and replacing it with a content-length header, which would make it closer to the C# code.
Dumb Guy
it returns "unsupported content exception" if I replace the content-type header with a content-length header. this is really weird. I still don't understand why the same web service accepts the post request sent via c# but not php, it doesn't make sense...
walter