php 向Web App发出POST请求会运行doPost,但响应中出现400错误

zaqlnxep  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(143)

场景:

我在AWS服务器上设置了一个WordPress网站。我正在向Google Apps Script Web应用程序发出POST请求,并尝试记录响应。我的PHP代码如下:

$request_obj = array(
    'data' => array(
        'client_id' => $client_id,
        'email' => $email
    )
);

$request_data = json_encode($request_obj);

$url = WEB_APP_URL;

$webSafe = str_replace(['+', '/'], ['-', '_'], base64_encode($request_data));

$args = array(
    'method' => 'POST',
    'headers' => array(
        'Content-Type' => 'application/json'
    ),
    'body' => array(
        'data' => $webSafe
    )
);

$response = wp_remote_post($url, $args);

字符串
在GCP中的Cloud Logs中,我可以看到请求正在被接收,doPost函数正在运行,这是根据函数的时间戳和日志

function doPost(e) {

  let request = JSON.parse(e.postData.contents)

  request = request.replace(/-/g, "+").replace(/_/g, "/").replace(/%3D/g, "=")
  request = forge.util.decode64(request) 

  console.log("request")
  console.log(request)

  const props = PropertiesService.getScriptProperties()
  props.setProperty(request.clientId, request.email)

  return ContentService.createTextOutput(`{
      "data": "success", 
      "code": 200
    }`).setMimeType(ContentService.MimeType.JSON)
  
}


但是我从Apps Script得到的响应就好像我最初提出的请求是无效的:

{
   "headers":{
      
   },
   "body":"<!DOCTYPE html>\n<html lang=en>\n  <meta charset=utf-8>\n  <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n  <title>Error 400 (Bad Request)!!1<\/title>\n  <style>\n    {margin:0;padding:0}html,code{font:15px\/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px} > body{background:url(\/\/www.google.com\/images\/errors\/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(\/\/www.google.com\/images\/branding\/googlelogo\/1x\/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_150x54dp.png) no-repeat 0% 0%\/100% 100%;-moz-border-image:url(\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n  <\/style>\n  <a href=\/\/www.google.com\/><span id=logo aria-label=Google><\/span><\/a>\n  <p><b>400.<\/b> <ins>That\u2019s an error.<\/ins>\n  <p>Your client has issued a malformed or illegal request.  <ins>That\u2019s all we know.<\/ins>\n",
   "response":{
      "code":400,
      "message":"Bad Request"
   },
   "cookies":[
      
   ],
   "filename":null,
   "http_response":{
      "data":null,
      "headers":null,
      "status":null
   }
}


尽管我可以在GCP中看到doPost日志:


的数据
我有另一个POST请求正在从WordPress站点,请求的细节是相同的,除了请求对象。工作有效载荷如下:

$request_obj = array(
  'data' => array(
    'nonce' => $nonce,
    'client_name' => $company_name,
    'product' => $payment_data->product_name,
    'license_information' => array(
      'subscription_id' => $license_data->id,
      'end_of_billing_cycle' => $payment_data->current_period_end,
      'last_payment' => $payment_data->current_period_start,
      'product' => $payment_data->product
    )
  )
);

问题:

在什么情况下,doPost仍然运行时会抛出400错误?我有点不知所措,因为通常情况下,如果HTTP请求无效,Apps Script代码不会运行。

xxhby3vn

xxhby3vn1#

我通过切换到curl并添加一个accept头来实现这个功能:

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json", "accept: application/json"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

if (curl_errno($ch)) {
  file_put_contents($log_file, 'Error occurred: ' . curl_error($ch), FILE_APPEND);
}

$response = curl_exec($ch);

字符串

相关问题