我遇到的问题是发出POST请求时,找不到任何外部类。问题仅存在于POST请求中。我做了一个完全正常的GET请求示例(可以使用SendGrid)所以我知道它不是composer autoloader的问题。 POST请求如何导致Fatal error: Class 'SendGrid' not found
?
<?php
$sendgrid = new SendGrid(REDACTED);
$email = new SendGrid\Email();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
var_dump($_POST);
if(isset($_POST['name'])){ $name = $_POST['name']; }
if(isset($_POST['email'])){ $from = $_POST['email']; }
if(isset($_POST['message'])){ $message = $_POST['message']; }
if(isset($_POST['g-recaptcha-response'])){ $captcha = $_POST['g-recaptcha-response']; }
$secret = REDACTED;
$recaptcha = new ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($captcha, $_SERVER['REMOTE_ADDR']);
if ($resp->isSuccess()) {
$email
->addTo('bar@example.com')
->setFrom($from)
->setSubject('New Contact')
->setText($message);
$sendgrid->send($email);
} else {
$errors = $resp->getErrorCodes();
}
}
elseif ($_SERVER['REQUEST_METHOD'] == 'GET') {
$name = 'Jim';
$from = 'foo@example.com';
$message = 'test message';
$email
->addTo('bar@example.com')
->setFrom($from)
->setSubject('New Contact: ' . $name)
->setText($message);
$sendgrid->send($email);
}
?>