使用PHP更新XML并使用SOAP返回.NET Web Service数据集

时间:2011-07-15 01:35:22

标签: php .net web-services soap simplexml

我使用PHP5和Codeigniter通过SOAP请求连接到.NET Web服务。我无法更新该数据集。这是我与Codeigniter合作的第一次经历(尽管在这里并不重要),SOAP,PHP SimpleXML类和.NET Web服务。例如,这是更新用户配置文件。我在回复时没有任何问题,但我不确定如何根据用户对个人资料的修改来更新此内容。

转储请求中的字符串是这样的(注意:我关注0,它是数据集的开头.1111是用户名,下一个1111是密码)

11111111 0RandyFloydGM1955-11-05T00:00:00-04:00317787129131789770001910 E. Markwood AvenueIndianapolisIN46227falsefalse

这给了我400 Bad Request错误。很明显,这是由于0和最后一个之间的空间1.通过做htmlspecialchars()我看到它看起来像是不需要xml声明。

<?xml version="1.0"?> <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-    msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><dsEmployee xmlns="http://SHSSrv/dsEmployee.xsd"><Employee diffgr:id="Employee1" msdata:rowOrder="0"><EmplId>0</EmplId><FirstName>Randy</FirstName><LastName>Floyd</LastName><MI>G</MI><Sex>M</Sex><DOB>1955-11-05T00:00:00-04:00</DOB><HomePhoneArea>317</HomePhoneArea><HomePhone>7871291</HomePhone><WorkPhoneArea>317</WorkPhoneArea><WorkPhone>8977000</WorkPhone><Address1>1920 E. Markwood Avenue</Address1><Address2/><City>Indianapolis</City><St>IN</St><ZIP>46227</ZIP><ReceiveNewsLetter>false</ReceiveNewsLetter><PagerArea/><PagerNo/><EmailAddress>randy@test.com</EmailAddress><SpanishContact>false</SpanishContact></Employee></dsEmployee></diffgr:diffgram> 

采取原始响应并将其发回,因为更新工作原理如下。 111111110RandyFloydGM1955-11-05T00:00:00-04:00317787129131789770001910 E. Markwood AvenueIndianapolisIN46227falsefalse

使用htmlspecialchars()看起来像这样(无XML声明):

<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-    msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><dsEmployee xmlns="http://SHSSrv/dsEmployee.xsd"><Employee diffgr:id="Employee1" msdata:rowOrder="0"><EmplId>0</EmplId><FirstName>Randy</FirstName><LastName>Floyd</LastName><MI>G</MI><Sex>M</Sex><DOB>1955-11-05T00:00:00-04:00</DOB><HomePhoneArea>317</HomePhoneArea><HomePhone>7871291</HomePhone><WorkPhoneArea>317</WorkPhoneArea><WorkPhone>8977000</WorkPhone><Address1>1920 E. Markwood Avenue</Address1><Address2/><City>Indianapolis</City><St>IN</St><ZIP>46227</ZIP><ReceiveNewsLetter>false</ReceiveNewsLetter><PagerArea/><PagerNo/><EmailAddress>randy@test.com</EmailAddress><SpanishContact>false</SpanishContact></Employee></dsEmployee></diffgr:diffgram>

以下是代码:

function employee_update_request()
{

    ini_set( 'soap.wsdl_cache_ttl' , 0 );

    //Get XML from the Employee Profile Request
    $response = $this->employee_profile_request();

    //Turn the string into an object to manipulate
    $dataset = simplexml_load_string($response->any);

    //Manipulate some data from the update form
    $dataset->dsEmployee->Employee->EmailAddress = "randy@test.com";
    $dataset->dsEmployee->Employee->Address1 = "1920 E. Markwood Avenue";
    $any = $dataset->saveXML();

    //Add back the string to the original response object returned from web service         
    $response->any = $any;

    //Get username and password for the params
    $username = $this->session->userdata('username');
    $password = $this->session->userdata('password');                       
    $params = array('sUserId' => $username, 'sPassword' => $password, 'dsEmployee' => $response);

    //SOAP Options
    $options = array(
            'soap_version'=>SOAP_1_1,
            'exceptions'=> 0,
            'trace'=> 1,
        'uri' => "http://www.w3.org/2003/05/soap-envelope"
            );

        //New soap client with options
        $client = new SoapClient('http://localhost/SHSSRV/SHSSrv.asmx?WSDL', $options);
        //Request the employee profile fromt the webservice, passing in credentials

        $update_request = $client->EmployeeUpdateRequest($params);
        $update_response = $update_request->EmployeeUpdateRequestResult;

        return $update_response;

}

我真的需要帮助,我需要弄清楚如何最好地对这些数据进行更新。我是否能够以某种方式删除声明,或者我是否应该以某种方式请求更改.NET Web服务?我无法直接访问,但如果有更好的方法,我可以与开发人员交谈。

谢谢!

1 个答案:

答案 0 :(得分:0)

我通过这样做解决了这个问题。我很想知道是否有更好的方法。

$no_xml_doctype = str_replace('<?xml version="1.0"?>' , '' , $any);
$trimmed = trim($no_xml_doctype);   
$response->any = $trimmed;

//Get username and password for the params
$username = rtrim($this->session->userdata('username'));
$password = rtrim($this->session->userdata('password'));                        
$params = array('sUserId' => $username, 'sPassword' => $password, 'dsEmployee' => $response);