发送REST数据后如何使用PHP头重定向



首先,我要感谢所有参与Stackoverflow的人,我总是在这个网站上找到解决方案,但到目前为止从未有机会帮助其他人。

我目前正在处理一个表单,它通过REST API将数据提交给我们的CRM,除了重定向之外,一切都正常工作。

通过修改以下代码设计的表单工作正常:https://codepen.io/SethCalkins/pen/JoXymK:

<form id="theForm" class="simform" autocomplete="off" method="post" action="">
<div class="simform-inner">
<ol class="questions">
<li>
<span class="genericform"><label for="first_name">Name:</label></span>
<input id="first_name" name="first_name" type="text" pattern="{3,}" required autofocus="true" autocomplete="off"/>
</li>
<li>
<span><label for="last_name">Surname:</label></span>
<input id="last_name" name="last_name" type="text" pattern="{3,}" required/>
</li>
<li>
<span><label for="phone">Phone:</label></span>
<input id="phone" name="phone" type="tel" data-validate="phone" inputmode="numeric" required/>
</li>
<li>
<span><label for="email">Email:</label></span>
<input id="email" name="email" type="email" data-validate="email" required/>
</li>
</ol><!-- /questions -->
<div class="controls">
<button class="next"><i class="svg-inline--fa fa-arrow-right></i></button>
<div class="progress"></div>
<span class="number">
<span class="number-current"></span>
<span class="number-total"></span>
</span>
<span class="error-message"></span> 
</div><!-- / controls -->
</div><!-- /simform-inner -->
<span class="final-message generic-popup-form-fade"><button class="popup-form-button-icon" type="submit" value="send">Submit</button></span>
</form>  
</div>    
<script>
// On form submit event
var theForm = document.getElementById( 'theForm' );
new stepsForm( theForm, {
onSubmit : function( form ) {
// hide form
classie.addClass( theForm.querySelector( '.simform-inner' ), 'hide' );
var messageEl = theForm.querySelector( '.final-message' );
$(".submit").css({"display":"inline"});
$(".uk-heading-medium").css({"text-align":"center"});
classie.addClass( messageEl, 'show' );
}
} );
</script>

用于发送的PHP在日志文件中写入并发送线索如下。它正确地写入日志,线索也到达CRM,只是重定向不起作用:

<?
/**
* Write data to log file.
*
* @param mixed $data
* @param string $title
*
* @return bool
*/
function writeToLog($data, $title = '') {
$log = "n------------------------n";
$log .= date("Y.m.d G:i:s") . "n";
$log .= (strlen($title) > 0 ? $title : 'DEBUG') . "n";
$log .= print_r($data, 1);
$log .= "n------------------------n";
file_put_contents(getcwd() . '/logfile.log', $log, FILE_APPEND);
return true;
}
$defaults = array('first_name' => '', 'last_name' => '', 'phone' => '', 'email' => '');
if (array_key_exists('saved', $_REQUEST)) {
$defaults = $_REQUEST;
writeToLog($_REQUEST, 'webform');
$queryUrl = 'https://queryurl/rest/';
$queryData = http_build_query(array(
'fields' => array(
"TITLE" => $_REQUEST['first_name'].' '.$_REQUEST['last_name'],
"NAME" => $_REQUEST['first_name'],
"LAST_NAME" => $_REQUEST['last_name'],
"STATUS_ID" => "NEW",
"PHONE" => array(array("VALUE" => $_REQUEST['phone'], "VALUE_TYPE" => "WORK" )),
"EMAIL" => array(array("VALUE" => $_REQUEST['email'], "VALUE_TYPE" => "WORK" )),
),
'params' => array("REGISTER_SONET_EVENT" => "Y")
));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl,
CURLOPT_POSTFIELDS => $queryData,
));
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result, 1);
writeToLog($result, 'webform result');
if ($result) {
header( 'Location: https://www.REDIRECTURL.com/thank-you/');
}

if (array_key_exists('error', $result)) echo "Error al enviar la solicitud: ".$result['error_description']."<br/>";
}
?>

我试着添加标题,包括:

header( 'Location: https://www.REDIRECTURL.com/thank-you/');

我在"?>"之后查找了空白,代码似乎没有任何错误。

不确定我错过了什么。。

对于任何想知道的人,我已经通过将页眉(''(重定向和所有其他php代码移动到页眉而不是页面底部(页脚(来解决了这个问题。

现在可以正常工作。

最新更新