开发者

Updating XML with PHP and returning to .NET Web Service dataset with SOAP

I am using PHP5 and Codeigniter to connect to a .NET web service through SOAP requests. I'm having trouble making an update to that dataset. This is my first experience working with Codeigniter (although doesn't factor here much), SOAP, PHP SimpleXML class, and .NET web services in general. For example, this is to update a user profile. I don't have any problems getting responses but I'm unsure how to update this based on the user's edits to the profile.

My string from the dumping request is this (Note: I'm concerned with the 0, which is the start of the dataset. The 1111 is username and the next 1111 is a password)

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

This gives me a 400 Bad Request error. It seems obvious that is due to the space between the 0 and the last 1. By doing htmlspecialchars() I see that it looks like it is the xml declaration isn't needed.

<?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> 

Taking the original response and just sending it back as update works like this. 111111110RandyFloydGM1955-11-05T00:00:00-04:00317787129131789770001910 E. Markwood AvenueIndianapolisIN46227falsefalse

And with htmlspecialchars() looks like this (No XML declaration):

<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>

Here is the code:

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'=>SO开发者_如何学PythonAP_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;

}

I really need help, I need to figure out how best to make updates to this data. Am I able to get the declaration stripped out somehow, or should I request that the .NET web service be changed in some way? I don't have access directly to that but I can talk to the developer if there is a better way all together.

Thanks!


I've solved this by doing this. I'd love to know if there is a better way though.

$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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜