如何使用PHP在Dynamics 365中添加新联系人

7
在 Joomla 应用程序中,我获取到一个用户信息如下,然后需要通过 Dynamics 365 的 REST API 将用户信息保存为联系人。
                    $user = JFactory::getUser();
                    $username = $user->username;
                    $name = $user->name;

我查看了与Web API和REST API有关的Dynamics文档,例如这篇这篇,但它们都没有提供关于如何调用API添加新联系人的有用信息。目前,我通过此URL连接到Dynamics 365 Web应用程序:http://example.com:8088/mysite/api/data/v8.2。链接的文章也谈到了REST API,但仅限于查询。我正在寻找使用REST API向Dynamics CRM发送数据的方法。

可能是通过PHP API访问MS Dynamics CRM的重复问题。 - Arun Vinoth-Precog Tech - MVP
@ArunVinoth,链接的帖子只讨论了查询,但我想发布数据。 - Mehdi Haghgoo
你说你已经通过URL连接了。 你用我回答中的有效载荷测试过CRM REST API URL吗?有任何特定的阻碍吗? - Arun Vinoth-Precog Tech - MVP
是的@ArunVinoth,在本地部署的Dynamics 365上无法工作。 - Mehdi Haghgoo
从你之前的问题 https://stackoverflow.com/q/51694272/7920473 中我所学到的是,你在调用API的认证部分存在问题,但这是针对这个特定问题的可用代码答案。 - Arun Vinoth-Precog Tech - MVP
1个回答

6
使用crm webapi创建联系人的有效载荷将如下所示:了解更多
POST [Organization URI]/api/data/v8.2/contacts HTTP/1.1
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json

{
    "firstname": "Arun",
    "lastname": "Vinoth"
}

很抱歉,我不是来自PHP背景,但这个链接可以帮助你。

更新:
我稍微浏览了一下,在SO答案中找到了以下代码示例。将[Organization URI]替换为CRM网址,例如https://testorg.crm.dynamics.com

$url = '[Organization URI]/api/data/v8.2/contacts';
$data = array('firstname' => 'Arun', 'lastname' => 'Vinoth');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);

我从哪里可以获取[组织URI]?我已经有https://portal.azure.com/账户和所有的东西。 - Adeel Shekhani
@AdeelShekhani 这是你的 CRM 组织 URL。例如:“https://devcrm.crm.dynamics.com”。 - Arun Vinoth-Precog Tech - MVP
谢谢,我找到了。您能告诉我如何获取、更新、插入联系人、用户等吗?文档中到处都有/api/data/v8.0/accounts等内容。但我找不到如何授权的说明。现在,如果我尝试获取联系人,它会返回401错误,并抱怨WWW-Authenticate。 - Adeel Shekhani
@AdeelShekhani,基本上是一样的,只需在Web API端点中切换模式名称并使用正确的属性模式名称。尝试使用CRM REST Builder来组合查询。对于身份验证,您必须使用Azure应用程序ID注册和令牌方法。从Postman或其他工具开始进行初始设置和测试。 - Arun Vinoth-Precog Tech - MVP
有没有任何网址可以找到任何指导。我正在使用Postman进行授权和https://login.microsoftonline.com/{tenant}/authorize,POST正文中带有client_id,但我得到的响应是“登录您的帐户”。还有“请求正文必须包含以下参数:\u0026#39;client_id\u0026#39;” - Adeel Shekhani
1
@AdeelShekhani 谷歌是最好的,看看这个链接 https://rajeevpentyala.com/2019/02/13/step-by-step-postman-tool-to-test-dynamics-365-online-web-api/ - Arun Vinoth-Precog Tech - MVP

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