Office365 PHP开发自定义Calendar

一、前期资源准备

@Office365官网申请一个超管账号,即新开一个租户。

@API调用参考地址:https://docs.microsoft.com/zh-cn/graph/api/user-post-events?view=graph-rest-1.0&tabs=http

 

二、核心代码

class CalendarService
{


    private $admin_booking_email = false;
    private $api_create_events = false;
    private $api_edit_events = false;
    private $api_delete_events = false;
    public function __construct()
    {
        $BASE_END_POINT = 'https://graph.microsoft.com/v1.0';
        $this->admin_booking_email = 'xxxx@domain.com';
        $this->api_create_events = $BASE_END_POINT."/users/{$this->admin_booking_email}/calendar/events";
        $this->api_edit_events = $BASE_END_POINT."/users/{$this->admin_booking_email}/calendar/events/%s";
        $this->api_delete_events = $BASE_END_POINT."/users/{$this->admin_booking_email}/calendar/events/%s";
    }

    public function create_events($event)
    {
        //https://docs.microsoft.com/zh-cn/graph/api/user-post-events?view=graph-rest-1.0&tabs=http
        /*
        $subject = 'Clinic Booking';
        $body = ['contentType'=>'HTML','content'=>'Does late morning work for you?'];
        $start = ['dateTime'=>'2017-04-16T12:00:00','timeZone'=>'Pacific Standard Time'];
        $end = ['dateTime'=>'2017-04-16T14:00:00','timeZone'=>'Pacific Standard Time'];
        $location = ['displayName'=>'Clinic London'];
        $attendee = [
            'type'=>'required',
            'emailAddress'=>[
                'address'=>'samanthab@contoso.onmicrosoft.com',
                'name'=>'Samantha Booth',
            ]
        ];
        $attendees = array();
        $attendees[] = $attendee;
        $event = [
            'subject'=> $subject,
            'body'=> $body,
            'start'=> $start,
            'end'=> $end,
            'location'=> $location,
            'attendees'=> $attendees
        ];*/
        //print_r(json_encode($event));exit();
        //print_r($event);exit();
        $eventData = $this->curlPostCalendar($this->api_create_events , json_encode($event));
        return $eventData;
    }



    private function curlPostCalendar($url,$postFields){
        $token = $this->GetAppOnlyAccessToken();
        $ch = curl_init ();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                "Authorization:Bearer {$token}",
                'SdkVersion:postman-graph/v1.0',
                //'Prefer:outlook.timezone="Europe/London"',
                //'Prefer:outlook.timezone="UTC"',
                'Content-Type: application/json; charset=utf-8'
            )
        );
        //curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //如果报错 name lookup timed out 报错时添加这一行代码
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt( $ch, CURLOPT_TIMEOUT,60);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec ( $ch );
        $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($rsp==201 ) {
            //从返回值提取ID
            $res = json_decode($ret,true);
            return $res['id'];
        }else{
            return false;
        }
    }



    public function GetAppOnlyAccessToken()
    {
        $TenantID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";  //租户标识,创建应用程序即可见;
        $getTokenUrl = "https://login.microsoftonline.com/{$TenantID}/oauth2/v2.0/token";

        $ClientID = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"; //客户端标识,通过【证书和密码】获取客户端标识和密码
        $ClientSecret = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; //客户端密码,通过【证书和密码】获取客户端标识和密码
        $postFields = array(
            'grant_type'=>'client_credentials',
            'client_id'=>"{$ClientID}",
            'client_secret'=>"{$ClientSecret}",
            'scope'=>'https://graph.microsoft.com/.default'
        );
        $ch = curl_init ();
        curl_setopt( $ch, CURLOPT_URL, $getTokenUrl);
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                'SdkVersion:postman-graph/v1.0',
                'Content-Type: application/x-www-form-urlencoded'
            )
        );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
        curl_setopt( $ch, CURLOPT_TIMEOUT,60);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec( $ch );
        $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($rsp==200) {
            $response = json_decode($ret);
            $token = $response->access_token;
            return $token?$token:false;
        }else{
            $token = false;
        }
        return $token;
    }


    public function edit_events($event){
        $url = sprintf($this->api_edit_events,$event['id']);
        $result = $this->curlPatchCalendar($url, json_encode($event['event']));
        return $result;
    }


    private function curlPatchCalendar($url,$postFields){
        $token = $this->GetAppOnlyAccessToken();
        $ch = curl_init ();
        curl_setopt( $ch, CURLOPT_URL, $url);
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                "Authorization:Bearer {$token}",
                'SdkVersion:postman-graph/v1.0',
                //'Prefer:outlook.timezone="Europe/London"',
                //'Prefer:outlook.timezone="UTC"',
                'Content-Type: application/json; charset=utf-8'
            )
        );
        //curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //如果报错 name lookup timed out 报错时添加这一行代码
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PATCH");
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt( $ch, CURLOPT_TIMEOUT,60);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec ( $ch );
        $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($rsp==200) {
            return true;
        }else{
            //print_r($ret);
            return false;
        }
    }

    public function delete_events($eventId){
        $url = sprintf($this->api_delete_events,$eventId);
        $result = $this->curlDeleteCalendar($url);
        return $result;
    }


    private function curlDeleteCalendar($url){
        $token = $this->GetAppOnlyAccessToken();
        $ch = curl_init ();
        curl_setopt( $ch, CURLOPT_URL, $url);
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                "Authorization:Bearer {$token}",
                'SdkVersion:postman-graph/v1.0',
                //'Prefer:outlook.timezone="Europe/London"',
                //'Prefer:outlook.timezone="UTC"',
                'Content-Type: application/json; charset=utf-8'
            )
        );
        //curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //如果报错 name lookup timed out 报错时添加这一行代码
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "DELETE");
        curl_setopt( $ch, CURLOPT_TIMEOUT,60);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec ( $ch );
        $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($rsp==204) {
            return true;
        }else{
            print_r($ret);
            return false;
        }
    }



}

三、 调用

$event = [
            'subject'=> $subject,
            'body'=> $body,
            'start'=> $start,
            'end'=> $end,
            'location'=> $location
            /* 'attendees'=> $attendees*/
        ];

        $eventId = $this->addOutlookCalendar($event);


 public function addOutlookCalendar($event)
    {
        return resolve(CalendarService::class)->create_events($event);
    }

 

$event = [
            'subject' => $subject,
            'body' => $body,
            'start' => $start,
            'end' => $end
        ];
        $this->updateOutlookCalendar(array('id' => $id, 'event' => $event));
$this->deleteOutlookCalendar($eventId);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卿风拂山岗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值