繁体   English   中英

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

[英]How to use PHP header redirect after sending REST data?

首先我要感谢所有参与 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 aswel,只是重定向不起作用:

<?
/**
 * 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/');

我在“?>”之后寻找空格,代码似乎没有任何错误。

不知道我错过了什么..

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

现在正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM