使用PHP GAE中的cURL访问GAS脚本日历事件创建

时间:2015-10-17 11:20:40

标签: php google-app-engine curl google-apps-script google-calendar-api

我想在Google App Engine中使用 PHP(通过cURL到GAS)在Google日历中创建日历活动,我不想使用 Google PHP CLIENT API

我想使用 GAS(Google Apps脚本)创建日历活动。所以我使用 CURL在PHP GAE中调用GAS脚本。但是我收到了一些错误

在我的脚本下面:

1.index.php

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#cal_btn').click(function () {
                $.ajax({
                    type: "POST",
                    'url': "create_event.php",
                    data:{'option':'create'},
                    success: function(data){
                        alert(data);
                    },
                    error: function(data){
                        alert('error in getting'+JSON.stringify(data));
                    }
                });
            });
        });
    </script>
</head>
<body>
<input type="button" id="cal_btn" value="CLICK HERE" />
</body>
</html>

2.create_event.php

<?php
$url='https://script.google.com/macros/s/AKfycbx7yhm5mA0AgXcYfVJmuzTQOJlOfiPLc-qlajM2qrdWKtPCfQA/exec';
$data=array('flag'=>1,'calendar_id'=>'dmain.com_gacf9d74moinqjet0ss87q8f7k@group.calendar.google.com','startdate'=>'2015-10-14','startdate_starttime'=>'10:00:00','startdate_endtime'=>'10:30:00','enddate'=>'2015-10-31','enddate_starttime'=>'10:00:00','enddate_endtime'=>'10:30:00');
$headers = array("Content-Type:multipart/form-data");

$params = '';
foreach($data as $key=>$value)
    $params .= $key.'='.$value.'&';

$params = trim($params, '&');
$ch = curl_init();
$data=http_build_query($data);
curl_setopt($ch, CURLOPT_URL, $url.'?'.$params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
$response = curl_exec($ch);
curl_close($ch);
print_r($response);

3.Calendar.gs

//CONVERT INPUT DATE TO CREATE CALENDAR EVENT
function CalenderTime_Convertion(calenderIDcode,CSRC_startdate, CSRC_startdate_starttime, CSRC_startdate_endtime)
{
  var cal = CalendarApp.getCalendarById(calenderIDcode); 
  var stime_in=CSRC_startdate_starttime.split(':');
  var stime_out=CSRC_startdate_endtime.split(':');
  var startdate=CSRC_startdate.split('-');//START DATE
  var month = startdate[1];
  var day = startdate[2];
  var year = startdate[0];
  var startdate = month + '/' + day + '/' + year;//string format of startdate
  var startdate1 = month + '/' + day + '/' + year;//string format of startdate
  startdate=new Date(startdate)
  startdate1=new Date(startdate1)
  var enddate=CSRC_startdate.split('-');
  var month1 = enddate[1];
  var day1 = enddate[2];
  var year1 = enddate[0];
  var enddate = month1 + '/' + day1 + '/' + year1;//string format of enddate
  var enddate1 = month1 + '/' + day1 + '/' + year1;//string format of enddate
  enddate=new Date(enddate)
  enddate1=new Date(enddate1)
  //CHECKIN DELETION
  var checkin=startdate;
  var checkin1=startdate1;
  var starttime=checkin.setHours(checkin.getHours() + stime_in[0]);
  var start_time=checkin.setMinutes(checkin.getMinutes() + stime_in[1]);
  var startdate_deletion=new Date(start_time);
  var starttime1=checkin1.setHours(checkin1.getHours() + stime_out[0]);
  var start_time1 = checkin1.setMinutes(checkin1.getMinutes() + stime_out[1]);//assigning time(hour) to enddate
  var startdate1_deletion=new Date(start_time1);
  var returnvalues=[startdate_deletion,startdate1_deletion];
  return returnvalues;
}
//Function

function calendarcreation(calenderIDcode,startdate,startdate_starttime,startdate_endtime,enddate,enddate_starttime,enddate_endtime){
  try{

    var cal = CalendarApp.getCalendarById(calenderIDcode) ;
    var initialsdate=startdate;
    var initialedate=enddate;
    var startevents=CalenderTime_Convertion(calenderIDcode,startdate, startdate_starttime, startdate_endtime);

    cal.createEvent('CHECKIN', startevents[0], startevents[1])
    var endevents=CalenderTime_Convertion(calenderIDcode,enddate,enddate_starttime,enddate_endtime)
    var c= cal.createEvent('CHECKOUT', endevents[0], endevents[1])
    return 'Event created successfully';
  }
  catch(err){
    return 'SCRIPT ERROR' + err;
  }
}

4.app.yaml

application: application name
version: 1
runtime: php55
api_version: 1

handlers:


- url: /create_event.php
  script: create_event.php


- url: .*
  script: index.php
  login: required

我在GAE中部署了这个项目。主窗体有一个按钮。如果我点击那个按钮,它应该通过php中的ajax调用GAS脚本。

但是我得到了这个Plz参考下面的图片。

{{3}}

这是警报中的内容,供您参考:

<!DOCTYPE html><html lang="en">  <head>  <meta charset="utf-8">  <meta content="width=300, initial-scale=1" name="viewport">  <meta name="description" content="Google Drive is a free way to keep your files backed up and easy to reach from any phone, tablet, or computer. Start with 15GB of Google storage – free.">  <meta name="google-site-verification" content="LrdTUW9psUAMbh4Ia074-BPEVmcpBxF6Gwf0MSgQXZs">  <title>Meet Google Drive – One place for all your files</title>  <style>  @font-face {  font-family: 'Open Sans';  font-style: normal;  font-weight: 300;  src: local('Open Sans Light'), local('OpenSans-Light'), url(//fonts.gstatic.com/s/opensans/v13/DXI1ORHCpsQm3Vp6mXoaTYnF5uFdDttMLvmWuJdhhgs.ttf) format('truetype');}@font-face {  font-family: 'Open Sans';  font-style: normal;  font-weight: 400;  src: local('Open Sans'), local('OpenSans'), url(//fonts.gstatic.com/s/opensans/v13/cJZKeOuBrn4kERxqtaUH3aCWcynf_cDxXwCLxiixG1c.ttf) format('truetype');}  </style>  <style>  h1, h2 {  -webkit-animation-duration: 0.1s;  -webkit-animation-name: fontfix;  -webkit-animation-iteration-count: 1;  -webkit-animation-timing-function: linear;  -webkit-animation-delay: 0;  }  @-webkit-keyframes fontfix {  from {  opacity: 1;  }  to {  opacity: 1;  }  }  </style><style>  html, body {  font-family: Arial, sans-serif;  background: #fff;  margin: 0;  padding: 0;  border: 0;  position: absolute;  height: 100%;  min-width: 100%;  font-size: 13px;  ...2Fs%2FAKfycbx7yhm5mA0AgXcYfVJmuzTQOJlOfiPLc-qlajM2qrdWKtPCfQA%2Fexec%3Fflag%3D1%26calendar_id%3Dexpatsint.com_gacf9d74moinqjet0ss87q8f7k%40group.calendar.google.com%26startdate%3D2015-10-14%26startdate_starttime%3D10%3A00%3A00%26startdate_endtime%3D10%3A30%3A00%26enddate%3D2015-10-31%26enddate_starttime%3D10%3A00%3A00%26enddate_endtime%3D10%3A30%3A00\x26followup=https%3A%2F%2Fscript.google.com%2Fmacros%2Fs%2FAKfycbx7yhm5mA0AgXcYfVJmuzTQOJlOfiPLc-qlajM2qrdWKtPCfQA%2Fexec%3Fflag%3D1%26calendar_id%3Dexpatsint.com_gacf9d74moinqjet0ss87q8f7k%40group.calendar.google.com%26startdate%3D2015-10-14%26startdate_starttime%3D10%3A00%3A00%26startdate_endtime%3D10%3A30%3A00%26enddate%3D2015-10-31%26enddate_starttime%3D10%3A00%3A00%26enddate_endtime%3D10%3A30%3A00\x26service=wise\x26passive=1209600');  </script><script>  (function(){  var signinInput = document.getElementById('signIn');  gaia_onLoginSubmit = function() {  try {  gaia.loginAutoRedirect.stop();  } catch (err) {  // do not prevent form from being submitted  }  try {  document.bg.invoke(function(response) {  document.getElementById('bgresponse').value = response;  });  } catch (err) {  document.getElementById('bgresponse').value = '';  }  return true;  }  document.getElementById('gaia_loginform').onsubmit = gaia_onLoginSubmit;  var signinButton = document.getElementById('signIn');  gaia_attachEvent(window, 'load', function(){  gaia_scrollToElement(signinButton);  });  })();</script>  </body></html>

API CONSOLE LOG:

A
2015-10-15 22:19:38.893
200
66.32 KB
194 ms
/create_event.php
345.989.33.33 - mailname [15/Oct/2015:07:19:38 -0700] "POST /create_event.php HTTP/1.1" 200 67911 http://domain.com/ "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0" "domain.com" ms=194 cpu_ms=15 cpm_usd=0.007657818 instance=0054444c61b184393if7393893333 app_engine_release=1.9.27 trace_id=-

Plz帮助我。怎么了?这有可能用这种方式吗?我正在使用cURL调用其他GAS项目,例如G Docs,Sheets,除了这个G CAL之外它工作正常。

1 个答案:

答案 0 :(得分:0)

您必须将应用脚本项目部署为Web应用。您还必须在项目中实现函数doGet(e)和/或doPost(e)才能发出请求。

尝试按照documentation中的步骤操作。在此示例中,返回了一个html页面,但您也可以返回json或您需要的任何信息。

我建议尝试使用一个简单的测试项目来检查一切是否正常,然后添加日历代码。