Php 为什么文件内容从web服务中得不到任何东西?

Php 为什么文件内容从web服务中得不到任何东西?,php,web-services,json,file-get-contents,Php,Web Services,Json,File Get Contents,我们使用file_get_内容与web服务通信,web服务创建用户,如果成功,它将返回一个JSON对象,其中包含新创建用户的详细信息。 下面的代码显示了我们是如何做到这一点的,用户被成功创建,这意味着我们可以从后端看到它,但是,我们无法得到JSON响应,它什么也不返回 public function register(){ $username = "testing"; $email = "testingemail@test.com"; $password = "testp

我们使用file_get_内容与web服务通信,web服务创建用户,如果成功,它将返回一个JSON对象,其中包含新创建用户的详细信息。 下面的代码显示了我们是如何做到这一点的,用户被成功创建,这意味着我们可以从后端看到它,但是,我们无法得到JSON响应,它什么也不返回

public function register(){
    $username = "testing";
    $email = "testingemail@test.com";
    $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);
}
起初,我们认为web服务应该不返回任何内容,所以我们在c#中对其进行了测试,它工作得非常好,这意味着我们得到了类似{“stutas”:“successful”,“userCreated”:“true”}的create响应 以下是c#代码:


php代码中是否缺少任何内容或配置错误?

php函数
file\u get\u contents
将获取响应的全部内容。您不需要
$res=stream\u get\u contents($fp)
。响应已在
$fp

您可以这样做:

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

伟大的谢谢!我得到了响应,但这是一个java异常。既然传入的url和数据完全相同,为什么它会返回异常?为什么这在c#中工作得非常好?这可能与您的web后端有关。您可以尝试删除内容类型标头并将其替换为内容长度标头,这将使其更接近C#代码。如果我将内容类型标头替换为内容长度标头,则会返回“不支持的内容异常”。这真的很奇怪。我仍然不明白为什么同一个web服务接受通过c#发送的post请求,但不接受php,这是没有意义的…您是否验证了JSON确实是您认为它正在使用的
print_r(JSON_decode($userData))
?@Peter,我终于用curl解决了这个问题。curl一开始不起作用,因为php设置错误。一旦这个问题解决了,一切都很顺利。谢谢你
$fp = @file_get_contents($url,false,$context);
print_r($fp);