Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft CRM 3.0 web service via PHP Soap class

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 ?

like image 858
Ben Avatar asked Oct 21 '09 22:10

Ben


2 Answers

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">'.
        '<CallerGuid xmlns="http://schemas.microsoft.com/crm/2006/CoreTypes">00000000-0000-0000-0000-000000000000</CallerGuid></CallerId>' .
    '</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/">' .
    $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);
    }
}
like image 68
Ben Avatar answered Oct 22 '22 10:10

Ben


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

http://us2.php.net/xmlrpc_encode_request

like image 21
popester Avatar answered Oct 22 '22 10:10

popester