Problem consuming .NET web service in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasonok6
Forum Newbie
Posts: 3
Joined: Tue Sep 01, 2009 1:26 pm

Problem consuming .NET web service in PHP

Post by jasonok6 »

I work at a company where we develop in PHP as well as .NET. I have written a simple web service in C# that I need to consume via PHP. The problem is that no matter how I return the data from C# whether a multi dimensional array, an array of objects, a .NET datatable type etc... PHP maps ALL of the xml to a single variable. I am trying to figure out how to get the different variables in the XML mapped to multiple variables into PHP which I can then traverse. Within PHP we are currently consuming coldfusion as well as other php web services without any problems. Seems that php doesn't like .NET for some reason. Here is the code I am using to generate the request using PHP 5.2:

Code: Select all

<?php
function ws_consume($url,$parms,$action)
{
    $client = new SoapClient($url);
    return $client->__soapCall($action,$parms);
}
$ar['intPageNumber'] = 1; //not implemented yet on ws side
$ar['intRows'] = 10; //not implemented yet on ws side
$obj = ws_consume(STORE_WS,$ar,'CartUserReferences');
print_r($obj);
?>
Here is the result I am getting from php:

Code: Select all

stdClass Object
(
    [CartUserReferencesResult] => stdClass Object
        (
            [any] => <CustomerRecord><user_id>15256</user_id><customer_id>7036be16-8ce0-4e7a-b55b-9fcacf820d82</customer_id></CustomerRecord><CustomerRecord><user_id>15255</user_id><customer_id>2c115556-0737-4443-9897-5c836c133dfb</customer_id></CustomerRecord><CustomerRecord><user_id>15250</user_id><customer_id>6f020f6b-f6be-49eb-ad1e-860a815ace04</customer_id></CustomerRecord><CustomerRecord><user_id>15247</user_id><customer_id>00a10a0e-7bfb-435b-8e36-a51a1fbfdc1b</customer_id></CustomerRecord><CustomerRecord><user_id>15246</user_id><customer_id>9edff925-924b-4695-82e6-4fcbd90c8060</customer_id></CustomerRecord><CustomerRecord><user_id>15245</user_id><customer_id>174eddc3-a62d-4e90-8578-b59afaca69be</customer_id></CustomerRecord><CustomerRecord><user_id>15243</user_id><customer_id>5f55dcd0-cd73-497c-bc62-a2ddb9d08e07</customer_id></CustomerRecord><CustomerRecord><user_id>15242</user_id><customer_id>6ae29b9f-544b-4b5e-b90a-b94523f3c76e</customer_id></CustomerRecord><CustomerRecord><user_id>15229</user_id><customer_id>8797aa8d-0bf9-4be3-a14c-8cebcf61802a</customer_id></CustomerRecord><CustomerRecord><user_id>15227</user_id><customer_id>7164f0f0-3c98-4b99-a113-ef229840f678</customer_id></CustomerRecord><CustomerRecord><user_id>15226</user_id><customer_id>3a5ac983-7d00-4e7d-add0-b4da5d9e0317</customer_id></CustomerRecord>
 
        )
 
)
 
Here is the raw xml returned by the .NET service:

Code: Select all

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CartUserReferencesResponse xmlns="http://tempuri.org/"><CartUserReferencesResult><CustomerRecord><user_id>15256</user_id><customer_id>7036be16-8ce0-4e7a-b55b-9fcacf820d82</customer_id></CustomerRecord><CustomerRecord><user_id>15255</user_id><customer_id>2c115556-0737-4443-9897-5c836c133dfb</customer_id></CustomerRecord><CustomerRecord><user_id>15250</user_id><customer_id>6f020f6b-f6be-49eb-ad1e-860a815ace04</customer_id></CustomerRecord><CustomerRecord><user_id>15247</user_id><customer_id>00a10a0e-7bfb-435b-8e36-a51a1fbfdc1b</customer_id></CustomerRecord><CustomerRecord><user_id>15246</user_id><customer_id>9edff925-924b-4695-82e6-4fcbd90c8060</customer_id></CustomerRecord><CustomerRecord><user_id>15245</user_id><customer_id>174eddc3-a62d-4e90-8578-b59afaca69be</customer_id></CustomerRecord><CustomerRecord><user_id>15243</user_id><customer_id>5f55dcd0-cd73-497c-bc62-a2ddb9d08e07</customer_id></CustomerRecord><CustomerRecord><user_id>15242</user_id><customer_id>6ae29b9f-544b-4b5e-b90a-b94523f3c76e</customer_id></CustomerRecord><CustomerRecord><user_id>15229</user_id><customer_id>8797aa8d-0bf9-4be3-a14c-8cebcf61802a</customer_id></CustomerRecord><CustomerRecord><user_id>15227</user_id><customer_id>7164f0f0-3c98-4b99-a113-ef229840f678</customer_id></CustomerRecord><CustomerRecord><user_id>15226</user_id><customer_id>3a5ac983-7d00-4e7d-add0-b4da5d9e0317</customer_id></CustomerRecord></CartUserReferencesResult></CartUserReferencesResponse></soap:Body></soap:Envelope>
Thanks in advance!
Last edited by jasonok6 on Tue Sep 01, 2009 3:05 pm, edited 1 time in total.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Problem consuming .NET web service in PHP

Post by Darhazer »

You are using SOAP to access the service, but it seems that the response is not SOAP?

Anyway, take a look at SimpleXML for parsing the xml response
jasonok6
Forum Newbie
Posts: 3
Joined: Tue Sep 01, 2009 1:26 pm

Re: Problem consuming .NET web service in PHP

Post by jasonok6 »

Looks like I managed to paste the original response returned by the web service incorrectly, I have pasted the real returned response as well as edited th original:

Code: Select all

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CartUserReferencesResponse xmlns="http://tempuri.org/"><CartUserReferencesResult><CustomerRecord><user_id>15256</user_id><customer_id>7036be16-8ce0-4e7a-b55b-9fcacf820d82</customer_id></CustomerRecord><CustomerRecord><user_id>15255</user_id><customer_id>2c115556-0737-4443-9897-5c836c133dfb</customer_id></CustomerRecord><CustomerRecord><user_id>15250</user_id><customer_id>6f020f6b-f6be-49eb-ad1e-860a815ace04</customer_id></CustomerRecord><CustomerRecord><user_id>15247</user_id><customer_id>00a10a0e-7bfb-435b-8e36-a51a1fbfdc1b</customer_id></CustomerRecord><CustomerRecord><user_id>15246</user_id><customer_id>9edff925-924b-4695-82e6-4fcbd90c8060</customer_id></CustomerRecord><CustomerRecord><user_id>15245</user_id><customer_id>174eddc3-a62d-4e90-8578-b59afaca69be</customer_id></CustomerRecord><CustomerRecord><user_id>15243</user_id><customer_id>5f55dcd0-cd73-497c-bc62-a2ddb9d08e07</customer_id></CustomerRecord><CustomerRecord><user_id>15242</user_id><customer_id>6ae29b9f-544b-4b5e-b90a-b94523f3c76e</customer_id></CustomerRecord><CustomerRecord><user_id>15229</user_id><customer_id>8797aa8d-0bf9-4be3-a14c-8cebcf61802a</customer_id></CustomerRecord><CustomerRecord><user_id>15227</user_id><customer_id>7164f0f0-3c98-4b99-a113-ef229840f678</customer_id></CustomerRecord><CustomerRecord><user_id>15226</user_id><customer_id>3a5ac983-7d00-4e7d-add0-b4da5d9e0317</customer_id></CustomerRecord></CartUserReferencesResult></CartUserReferencesResponse></soap:Body></soap:Envelope>
jasonok6
Forum Newbie
Posts: 3
Joined: Tue Sep 01, 2009 1:26 pm

Re: Problem consuming .NET web service in PHP

Post by jasonok6 »

Solved my own problem. My php.ini settings were caching WSDLs and i had originally tested the webservice returning back a single value. Once I changed the web service so that it was returning complex data php was still using the rules from the old WSDL. Turned off soap caching and it started working as expected.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Problem consuming .NET web service in PHP

Post by JNettles »

Consuming .NET web services was a huge pain for me and my company as well. .NET generally doesn't play nicely with PHP, especially when passing the incredibly enormous datatable which, while highly useful inside .NET, is nearly an impenetrable fog when trying to parse it as a SOAP result in any other language. I'm working on a .NET communication layer which rests between a .NET web service and your PHP. Its turned into a pretty strong class - it parses datatable information (though I have to do an extra process to do datatype transformation, since PHP reads everything as a string) and is a fairly handy SOAP manager as well.

Unfortunately, my communication layer was produced on government time and I need permission from the state to release it as open source. Still waiting on that...... :(
Post Reply