将数据从Google App脚本发送到php网站

时间:2017-08-17 13:58:00

标签: php google-apps-script

尝试将数据从Google App Script发送到php网站,该网站从我的数据库发送电子邮件。在大多数日期,我通过网址(example.com/example.php?id=test&id2=test2&id3=test3)发送它没有问题。

对于大多数数据来说,除了第三个参数之外没有问题,给定值比其他两个更长,并且也可能包含换行符。我的想法是将第三个(或所有值)作为JSON对象发送。 到目前为止,我的Google AppScript代码:

function  sendData(){
  var options = {'text' : 'A longer example text'}  ;
  try {
    var url = "https://example.com/example.php?id1=test1&id2=test2&id3="+options;
    var httpRequest = UrlFetchApp.fetch(url, options).getContentText();
  }
  catch(e){
    Logger.log(e);
  }
  return;
}

我的PHP代码:

<?php
require 'includes/functions.php';
include_once 'config.php';
include 'ChromePhp.php';

$email = $_GET["id1"];
$name = $_GET["id2"];
$text = $_GET["id3"];

$json =     json_decode($text, true);
ChromePhp::log($json->text);

if(strpos($text, "\n") !== FALSE) {
    $text = str_replace("\n", "<br>", $text);
} 

$replace_to_standard=[$name, $text];
$replace_standard =['nametochange', 'textochange'];
$delivery_added = str_replace($replace_standard, $replace_to_standard, file_get_contents('standardMail.html'));

echo "" . $delivery_added . "";

$m = new MailSender;
$m-> sendMail($email, $email, $delivery_added, "Mail All");

Chrome记录器将记录“null”

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

以AJAX帖子为例,您可以使用Google应用脚本的POST功能向PHP文件发送帖子请求,并使用PHP的$ _POST函数处理它。

试试你GAS:

<?php
require 'includes/functions.php';
include_once 'config.php';
include 'ChromePhp.php';

 
//Receive the RAW post data via the php://input IO stream.
$content = file_get_contents("php://input");

$email = $_POST["email"];
$text= $_POST["text"];


if(strpos($text, "\n") !== FALSE) {
    $text = str_replace("\n", "<br>", $text);
} 

$replace_to_standard=[$name, $text];
$replace_standard =['nametochange', 'textochange'];
$delivery_added = str_replace($replace_standard, $replace_to_standard, file_get_contents('standardMail.html'));

echo "" . $delivery_added . "";

$m = new MailSender;
$m-> sendMail($email, $email, $delivery_added, "Mail All");

在PHP中捕获您的POST请求并按如下方式编辑它:

$someCity = post("city",'none');
session('city',$someCity);