使用Web服务访问Dynamics CRM 2011的PHP方法

17

我需要通过Web服务访问CRM 2011中的潜在客户(创建新的潜在客户并获取列表)。我已经使用c#/asp.net创建了一个应用程序(它可以工作),但现在我必须用PHP完成它,但遇到了困难。

我尝试过https://code.google.com/p/php-dynamics-crm-2011/,但它不起作用,因为它只支持联合身份验证而我的是活动目录。

我尝试使用nusoap进行连接,但非常混乱。

我生成了发现服务和组织服务的类,使用wsdl2php:http://www.urdalen.no/wsdl2php/,但我不知道如何使用这些类。

有人有使用这些类的示例吗?


请查看以下问题:http://stackoverflow.com/questions/16890036/dynamically-pull-data-from-dynamics-crm-online - Guido Preite
我不明白,CRM已经提供了Web服务,我需要知道如何使用PHP来消费它们。 - jvo
我的建议仅供参考,您可以使用任何语言来调用 CRM 2011 Web 服务,但更简单的方法是创建一个充当代理的 Web 服务。 - Guido Preite
3
您可以使用 PHP OData 终端和 OData for PHP SDK。否则,假设您在其他方面都做得正确,那么您将以与从 JavaScript 中消耗它们相同的方式在 PHP 中使用它们。 - Mike_Matthews_II
1个回答

5

MSCRM 2013和可能的2011版本在身份验证Web服务时使用NTLM。

对于数据查询,您可以使用url编码的FetchXML。

http://msdn.microsoft.com/en-us/library/gg328117.aspx

您可以通过在高级搜索中导出XML并使用RetrieveMultiple方法执行查询来从CRM获取正确的XML,例如。

我将添加一个带有SOAP信封和CURL POST查询的示例,使用NTLM进行身份验证。

<?php

$soap_envelope = <<<END
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <RetrieveMultiple xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <query i:type="a:FetchExpression" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
        <a:Query>&lt;fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'&gt;
          &lt;entity name='contact'&gt;
            &lt;attribute name='fullname' /&gt;
            &lt;attribute name='parentcustomerid' /&gt;
            &lt;attribute name='telephone1' /&gt;
            &lt;attribute name='emailaddress1' /&gt;
            &lt;attribute name='contactid' /&gt;
            &lt;order attribute='fullname' descending='false' /&gt;
            &lt;filter type='and'&gt;
              &lt;condition attribute='ownerid' operator='eq-userid' /&gt;
              &lt;condition attribute='statecode' operator='eq' value='0' /&gt;
            &lt;/filter&gt;
          &lt;/entity&gt;
        &lt;/fetch&gt;</a:Query>
      </query>
    </RetrieveMultiple>
  </s:Body>
</s:Envelope>
END;

$soap_action = 'http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/RetrieveMultiple';
$req_location = 'http://crm.server.local/YourOrganization/XRMServices/2011/Organization.svc/web';

$headers = array(
  'Method: POST',
  'Connection: Keep-Alive',
  'User-Agent: PHP-SOAP-CURL',
  'Content-Type: text/xml; charset=utf-8',
  'SOAPAction: "'.$soap_action.'"'
);

$user = 'YOURDOMAIN\YOURUSERNAME';
$password = '**********';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $req_location);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $soap_envelope);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password);
$response = curl_exec($ch);

if(curl_exec($ch) === false)
{
  echo 'Curl error: ' . curl_error($ch);
}
else
{
  var_dump($response);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接