views:

712

answers:

6

I would like to create new contacts and leads using php. I can't quite figure out how to call the methods of the mscrm 3 web service.

The php soap class seems quite simple to use. I am able to connect and authenticate to the crm web service and get a list of available functions however I am unsure how to go about calling them.

I have seen examples for mscrm 4.0 which seem to involve masses of XML including soap headers and envelopes.

I am under the impression that using a soap class bypasses this and will write all the extra xml for me so all I need to do is call a function with an array of parameters?

Am I completely wrong here ?

Has anyone done this with mscrm 3 that can provide some sample code, or perhaps give me a few pointers as how to correctly call the Create() method ?

+1  A: 

Any decent SOAP toolkit will automagically spit out the correct XML. Check out this guy:

http://us2.php.net/xmlrpc%5Fencode%5Frequest

popester
I am using the PHP Soap class, where I am really having trouble is Knowing what parameters to pass to the create() method an formatting them correctly.
Ben
+2  A: 

I have been able to get this working by using Nusoap and after construction the XML message as a series of strings using the send method instead of call. This now works as expected. It seemed that using the call method was returning different XML than what was required by the ms crm 3 web service.

Ben
A: 

Ben,

I am having the very same problem. U happen to have found a solution for it?

Greetings,

Marc

marc
Yes i have this working... I am quite drunk right now but will post some working code tomorrow for you!!
Ben
+1  A: 
require_once ('/var/mtp/lib/vendor/nusoap/lib/nusoap.php');

$login ='domain\username';
$pass ='password';
$useCURL = true;

$client = new nusoap_client('http://server:5555/mscrmservices/2006/crmservice.asmx?wsdl', 'wsdl');
$client->setCredentials($login, $pass, 'ntlm');
$client->setUseCurl($useCURL);
$client->useHTTPPersistentConnection();
$client->soap_defencoding = 'UTF-8';

$err = $client->getError();
if ($err) {
    echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
    echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
    exit();
}

$soapHeader='<soap:Header>' .
        '<CallerId xmlns="http://schemas.microsoft.com/crm/2006/WebServices"&gt;'.
        '<CallerGuid xmlns="http://schemas.microsoft.com/crm/2006/CoreTypes"&gt;00000000-0000-0000-0000-000000000000&lt;/CallerGuid&gt;&lt;/CallerId&gt;' .
    '</soap:Header>';

$soapBody='<soap:Body>' .
    '<entity xmlns="http://schemas.microsoft.com/crm/2006/WebServices"  xsi:type="lead">' .
        '<ownerid type="Owner">2408c7dc-c0a3-dd11-b3cd-001a4bd3009a</ownerid>' .         
        '<firstname>Fred</firstname>' .
        '<lastname>Bloggs</lastname>' .
    '</entity>' .
    '</soap:Body>';


$xml = '<?xml version="1.0" encoding="utf-8"?>' .
    '<soap:Envelope' .          
        ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' .
        ' xmlns:xsd="http://www.w3.org/2001/XMLSchema"' .
        ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;' .
    $soapHeader .
    $soapBody .
    '</soap:Envelope>';

//SOAP call
$result = $client->send($xml,'http://schemas.microsoft.com/crm/2006/WebServices/Create' );

//result
if ($client->fault) { //check for fault
    echo '<p><b>Fault: ';        
    print_r($result);        
    echo '</b></p>';
}

else { //no fault
    $err = $client->getError();
    if ($err) { // error
        echo 'Error: ' . $err . '';
        echo "\n\n# # # # # # # Request # # # # # # #\n";
        var_dump($client->request);
        echo "\n\n# # # # # # Response # # # # # # #\n";
        var_dump($client->response);
    }
    else { // display the result
    print_r($result);
    }
}
Ben
A: 

Hi guys, I was also struggling to get Dynamics CRM SOAP working with PHP but after a while I managed to get it working; http://www.ifc0nfig.com/working-with-microsoft-dynamics-crm-4-0-soap-interface-with-php-and-nusoap/ - You can download a small class I created which may be of use :)

eth0
A: 

I am trying to do this with MSCRM 3 as well, the above example code works for authentication but fails after that with the following output.

"Array ( [faultcode] => soap:Client [faultstring] => Server was unable to process request. [detail] => Array ( [error] => Array ( [code] => 0x80041102 [description] => The specified entity was not found. [type] => Platform ) ) ) "

Canadaka
Are you setting your own owner id ?? The one in the sample wont work.
Ben