如何使用SOAP将对象从PHP发送到Java web服务


How send an object from PHP to Java web service using SOAP?

我有一个正在运行的web服务(使用EclipseLink作为JPA提供者),并且希望使用SOAP从PHP调用更新数据库中数据的方法

web服务中的一种方法可能看起来像这样:

public void updatePerson(Person p){
   EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersonLib");
   EntityManager em = emf.createEntityManager();
   if(!em.getTransaction().isActive()) {
      em.getTransaction().begin();
   }
   em.merge(p);
   em.getTransaction().commit();
}

在PHP中,我想我必须创建一个类型为stdClass的对象,并将其作为Person的参数发送。我说得对吗?但我无法使用以下代码行:

$client = new SoapClient("url.to.wsdl", array("trace" => 1));
$obj = new stdClass();
$obj->Person = new stdClass(); 
$obj->Person->personId = 1;
$obj->Person->name = "Peter";
$client->updatePerson($obj);

我不知道这是否是将对象从PHP发送到Java的正确方式(好吧,它在Java应用程序中调用方法updatePerson(Person p),但p不包含我在PHP中输入的数据)。

如果可能的话,请向我们展示WSDL文件。

通常,当我在PHP中使用SoapClient时,即使web服务需要一个对象,我也会使用数组,因此,不要创建新的stdClass,而是尝试发送以下数组:

$client = new SoapClient("url.to.wsdl");
$obj    = new array("personId" => 1, "name" => "Peter");
$client->updatePerson($obj);

这应该会向对象发送所需的数据。

希望能有所帮助。