未经授权:从PHP在线访问Web API Dynamics 365

时间:2017-12-18 10:42:48

标签: php curl dynamics-crm

我正在尝试使用PHP在线调用Dynamics CRM 365,但我仍然遇到同样的错误:

  

错误:状态为401失败,响应HTTP错误401 - 未经授权:拒绝访问,curl_error

这是我的代码

define('MS_CRM_URL', 'https://xxxx.crm4.dynamics.com');
define('MS_CRM_USER', 'xxxxx@xxxxx.onmicrosoft.com');
define('MS_CRM_PASS', 'xxxxxxxx');
$method = '/api/data/v8.2/contacts';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MS_CRM_URL . $method);
curl_setopt($ch, CURLOPT_USERPWD, MS_CRM_USER .':'. MS_CRM_PASS);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(

));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept' => 'application/json',
    'OData-MaxVersion' => '4.0',
    'OData-Version' => '4.0',
    'Content-Type' => 'application/json',
));

$server_output = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

$json = array();
if ((int)$status_code === 200) {
    $json = json_decode($server_output);
}

0 个答案:

没有答案