尝试使用 PHP 中的 API 在 Dynamics CRM 中创建潜在客户

Posted

技术标签:

【中文标题】尝试使用 PHP 中的 API 在 Dynamics CRM 中创建潜在客户【英文标题】:Trying to create lead in Dynamics CRM using API in PHP 【发布时间】:2017-05-04 01:27:30 【问题描述】:

我正在,但出现 400 html 错误。我的 Dynamics CRM 使用 Active Directory 身份验证。我可以使用 API 成功获取潜在客户列表,但无法创建。这是我的代码:

$xml = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
    <Create xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <entity xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
        <a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
          <a:KeyValuePairOfstringanyType>
            <b:key>Firstname/b:key>
            <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">TestTopic</b:value>
          </a:KeyValuePairOfstringanyType>
          <a:KeyValuePairOfstringanyType>
            <b:key>Lastname</b:key>
            <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">testName</b:value>
          </a:KeyValuePairOfstringanyType>
        </a:Attributes>
        <a:EntityState i:nil="true" />
        <a:FormattedValues xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
        <a:Id>00000000-0000-0000-0000-000000000000</a:Id>
        <a:LogicalName>lead</a:LogicalName>
        <a:RelatedEntities xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
      </entity>
    </Create>
  </s:Body></s:Envelope>';

$headers = array ( 
                    "POST " . "/Organization.svc" . " HTTP/1.1",
                    "Host: " . $host,
                    "Connection: Keep-Alive",
                    "Content-type: application/SOAP+xml; charset=UTF-8"
                    //,"Content-length: " . strlen ( $xml )
            );

$ch = curl_init ();
            curl_setopt ( $ch, CURLOPT_URL, $crm_url  . "/XRMServices/2011/Organization.svc" );
        //curl_setopt($ch, CURLOPT_HTTPHEADER, array('"POST " . "/Organization.svc" . " HTTP/1.1","Host: " . $host,"Connection: Keep-Alive","Content-type: application/json; charset=UTF-8"'));
            curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
            curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
            curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
            curl_setopt ( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
            curl_setopt ( $ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
            curl_setopt ( $ch, CURLOPT_USERPWD, "$username:$password");
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
            curl_setopt ( $ch, CURLOPT_POST, 1 );
            curl_setopt ( $ch, CURLOPT_POSTFIELDS, $xml );

            $response = curl_exec($ch);
            if( $response === false)  echo 'Curl error: ' . curl_error($ch);
            $status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );echo "Status: $status ";
            $response=json_decode($response, true);
            print_r($response);
            curl_close($ch);

【问题讨论】:

提问前请参考:***.com/help/how-to-ask 【参考方案1】:

此代码适用于使用 Active Directory 身份验证的 Dynamics crm on-premises 2015。

$host = 'orgcrm.org.com:5555'; // org crm url without https://
$organization = 'OrgName';
$crm_url = "https://$host/$organization/";    

         $headers = array(
                              'Method: POST',
                              'Connection: keep-alive',
                              'User-Agent: php-SOAP-CURL',
                              'Content-Type: application/json; charset=utf-8',
                              'Accept: application/json',
                              'Host: ' . $host);            

    $create = json_encode(array('FirstName' => $firstname, 
                                'LastName' => $lastname, 
                                'Telephone1' => $tel, 
                                'EMailAddress1' => $email), JSON_FORCE_OBJECT);         

            $ch = curl_init();
                curl_setopt ( $ch, CURLOPT_URL, $crm_url  . "/XRMServices/2011/OrganizationData.svc/LeadSet" );
                curl_setopt ( $ch, CURLOPT_USERPWD, "$username:$password");
                curl_setopt ( $ch, CURLOPT_POSTFIELDS, $create); 
                curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers);
                curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt ( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
                curl_setopt ( $ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
                //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

                $response = curl_exec($ch);
                if( $response === false)  echo 'Curl error: ' . curl_error($ch);
                $status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );echo "Status: $status ";

                $response1 = json_decode($response, true); 
                foreach ($response1 as $id) 
                        $guid = $id['LeadId'];                  
                        echo 'GUID: ' . $guid. ' ';
                               
                print_r($response1);

                curl_close($ch);

【讨论】:

以上是关于尝试使用 PHP 中的 API 在 Dynamics CRM 中创建潜在客户的主要内容,如果未能解决你的问题,请参考以下文章

标题中的Wordpress dynamic_sidebar阻止了主题其余部分的呈现

Agora.io DYNAMIC_USE_STATIC_KEY 生成的 RTC Token (php)

paypal批量支付api在php中的使用

项目中的web api知识总结

API使用dynamic 传参

API使用dynamic 传参