【问题标题】:rest php client unmarshalling exceptionrest php 客户端解组异常
【发布时间】:2017-11-23 06:48:26
【问题描述】:

任务是在 php 中构建一个 REST 客户端,发送和接收 XML(很奇怪,我知道)。

如果我向 REST-api 发送内容,我会收到 UnmarshallingException。

这是什么意思?

这是我的代码的一部分:

public function createOrganisationUnit($kvcObject){
    try{
        $restclient = new RestClient(Environment::getConfiguration()['rest_url']);
        $response = $restclient->execute(
            RestClient::REQUEST_TYPE_POST,
            '/rest/organisations/'.self::getOurID().'/organisationunits',
            $kvcObject->xmlOrganisationUnit()
        );
        echo 'createOrg<br>';var_dump($response);echo'<br>';
        if ($response['http_code'] == 201){         //created, return new ID
            return substr($response['body'], strrpos($response['body'], "/"));
        }elseif($response['http_code'] == 400){     //allready there, return found ID
            return self::getOrganisationUnitsID($kvcObject->getOrganisationUnit()->getBsnr());
        }else{                                      // error, return false
            return false;
        }
    }catch (Exception $e){
        print_r( $e->getMessage() ) . PHP_EOL;
    }   
}

$kvcConnect->xmlOrganisationUnit() 将 xml 作为字符串传递。 self::getOurID() 工作正常,将建议的 ID 作为整数提供。

我不使用 Symfony 或任何其他框架。

最好的问候 约翰内斯

【问题讨论】:

  • 产生这个错误的代码在哪里?
  • @Morgoth:我添加了一些代码。

标签: php rest exception


【解决方案1】:
try {
    FileInputStream inputStream = new FileInputStream(new File("your file"));
    AddRequest req = JAXB.unmarshal(inputStream, AddRequest.class);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

【讨论】:

  • 这段代码可能会回答这个问题,但是你能添加一些上下文来解释它为什么回答这个问题吗?
【解决方案2】:

我发现,我的 RestClass 中的 sprint("%s", $post_data) 是问题所在。

【讨论】:

    猜你喜欢
    • 2011-04-18
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多