7
votes

In a Joomla application I am getting a user info as follows and then I need to save the user info as a contact in a Dynamics 365 database through their REST API.

                    $user = JFactory::getUser();
                    $username = $user->username;
                    $name = $user->name;

I have looked up Dynamics documents around Web API and REST API like this and this, but none of them provide useful info how I can call the API to add a new contact. Currently, I am connecting to Dynamics 365 web application via this url: http://example.com:8088/mysite/api/data/v8.2. The linked post also talks about REST API, but only querying. I'm looking for a way to post data to Dynamics CRM using REST API.

1
@ArunVinoth the linked post is different in the sense it only talks about querying, but I want to post data.codezombie
You said you are already connecting by url. Did you test CRM REST API url with the payload from my answer? Any particular blocker?Arun Vinoth
Yes @ArunVinoth, it didn't work with on-premises Dynamics 365.codezombie
What I learnt from your other question stackoverflow.com/q/51694272/7920473 you have issues with authentication part in calling the API, but this is working code answer for this particular question.Arun Vinoth

1 Answers

6
votes

The payload to create Contact using crm webapi will look like this: Read more

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"
}

Sorry am not from PHP background, but this link may help you.

Update:
I browsed little bit. Found the below code sample from SO answer. Update the [Organization URI] with CRM URL, for ex. 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);