How to Add a New Contact in Dynamics 365 using PHP - php

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.

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);

Related

Can't get access token from Bigcommerce API, invalid client ID

I am trying to get the access token so I can start building an app that works with BigCommerce. I've been following the docs here: https://developer.bigcommerce.com/api/callback. I'm using the PHP client for Bigcommerce.
The response is HTTP/1.1 400 Bad Request {"error":"Invalid client id."}.
I swear I'm using the correct client id and client secret! Or at least they are what is displayed when I click "View Client ID" on my draft app in the developer portal.
What on earth am I doing wrong?
$request = $_REQUEST;
require_once 'vendor/autoload.php';
use Bigcommerce\Api\Connection;
$tokenUrl = "https://login.bigcommerce.com/oauth2/token";
$connection = new Connection();
$connection->verifyPeer();
$connection->useUrlencoded();
$response = $connection->post($tokenUrl, array(
"client_id" => "", //I won't type it here but it is correct
"client_secret" => "", //also correct
"redirect_uri" => "https://127.0.0.1/project-custom/oauth.php", //this is the Auth Callback URL
"grant_type" => "authorization_code",
"code" => $request["code"], //when I echo these variables out they work
"scope" => $request["scope"],
"context" => $request["context"],
));
print_r($connection->getLastError());
I figured it out!
I just removed the line $connection->useUrlencoded(); because it needed to be sent as "Content-Type: application/json" and I was sending it as "Content-Type: application/x-www-form-urlencoded"

NodeJitsu PHP Curl Json API - Restart an application

I have problem with use NodeJitsu API in PHP curl...
I want make php file which will be restart my application.
Here is NodeJistu API: https://www.nodejitsu.com/documentation/api/#restart-an-application
But i don't realy now how i can use it in php. Can you help me?
They use a simple REST API. You'll need to send an empty HTTP POST request to the url named in the docs. A request body isn't required for the restart action.
I don't have an account for testing there, but following their documentation it could look like this:
/* Login credentials */
$user = 'user';
$pass = 'secret';
/* Application id */
$application = 'foo';
/* Base url */
$baseUrl = 'https://www.nodejitsu.com';
// Create a context for the following HTTP request
$context = stream_context_create(
'http' => array(
'method' => 'POST',
'header' => 'Authorization: Basic '
. base64_encode("$user:$pass")
)
);
// Execute the HTTP request to restart the application
$url = "$baseUrl/apps/$user/$application/restart";
$response = file_get_contents($url, false, $context);
// Dump response
var_dump(json_decode($response));
You can use file_get_contents(), curl isn't required.

Facebook SDK PHP: send notifications

I have an API that serves Android, IOS and Windows Phone apps. The API works fine with Facebook Graph API. I'm trying to create a method that sends a Facebook notification to user, based on this example and Facebook SDK documentation. Here is my code:
public function sendNotification($uid, $msg, $link){
$postdata = "access_token=$this->access_token&href=$link&template=$msg";
$opts = ['http' => ['method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata]];
$context = stream_context_create($opts);
$url = "https://graph.facebook.com/$uid/notifications";
$r = file_get_contents($url, false, $context);
var_dump( $r );
die();
}
My Facebook class has uid, access_token, app_id and app_secret attributes. I should call:
$fb = new Facebook();
$fb->sendNotification($uid, 'Test message', 'http://test.com');
I have the following var_dump:
<b>Warning</b>:
file_get_contents(https://graph.facebook.com/100001795849314/notifications): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in <b>C:\xampp\htdocs\faceteen\api\Classes\Facebook.php</b> on line <b>144</b><br />
bool(false)
I'm running on localhost with Facebook Graph generated token (with manage_notifications perm). I don't want to use Facebook's class, but my own. What am I doing wrong?

Sending http request in oxwall

I am working on oxwall plugin. I am stuck at one problem .
I need to know how to send http request to url.
For example :
1) In joomla CMS we send request like this,
$http = new JHttp($options, $transport);
$response = $http->post($url, $data);
2)In drupal ,
$options = array(
'method' => 'POST',
'data' => $data,
'timeout' => 15,
'headers' => array('Content-Type' => 'application/json'),
);
$result = drupal_http_request($url, $option);
I want to know , what is the oxwall way of doing this task, Please help me or hint me which library to look out. If i am unable to find the solution will it be okay to use custom php code send request . Will it affect the performance of plugin ?
I don't think there exists exactly what you are looking for.
You would just use custom PHP code for this.

weibo oauth authentication

I used weibo oauth api in magento for connect user with weibo.
But now weibo is broken and it completely get token but display error
when we retrieve user data using authentication token.error is as follows..
i am using this code the use can successfully login but after that there is an error like this
[error_code] => 401
[error] => 40109:consumer_key_refused!
my code is here for after login\
$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret'] );
$ms = $c->home_timeline();
$me = $c->verify_credentials();
$ms = $c->show_user($userid);
I found weibo new oauth2.0 authentication api that solve my problem.Use this if any one have problem in weibo user authentication..
Weibo-Oauth2 and follow Application scenarios step.
For get access token,you need to use form POST method instead of GET.so you use this code.
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: text/xml\r\n"
)
);
$context = stream_context_create($opts);
$uri= 'https://api.weibo.com/oauth2/access_token?client_id='.WB_AKEY.'&client_secret='.WB_SKEY.'&grant_type=authorization_code&redirect_uri='.YOUR_REGISTERED_REDIRECT_URI.'&code='your authorization code;
$authkey1 = file_get_contents($uri,false,$context);
$decoded_auth1 = json_decode($authkey1,true);
And use this url to get authenticate user data..
$userinfo = file_get_contents("https://api.weibo.com/2/users/show.json?access_token=".$access_token."&uid=".$userid);
$decoded_userinfo = json_decode($userinfo, true);
Hope this help to anyone..
Use weibo_2 with oauth2. please.

Categories