简体   繁体   English

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

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

First of all I'd like to thank all the people participating in Stackoverflow, I always find solutions on this website but never got the opportunity to help someone else so far.首先我要感谢所有参与 Stackoverflow 的人,我总是在这个网站上找到解决方案,但到目前为止还没有机会帮助别人。

I'm currently working on a form, which submits data via REST API to our CRM and everything works as it should except for the redirect.我目前正在处理一个表单,它通过 REST API 向我们的 CRM 提交数据,除了重定向之外,一切都正常工作。

The form, which is designed by adapting the following code, is working correctly: https://codepen.io/SethCalkins/pen/JoXymK :通过修改以下代码设计的表单可以正常工作: 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>

The PHP used to send to write in the log file and send the lead is the following. PHP用来发送写入日志文件并发送的导数如下。 It writes the log correctly and the lead arrives to the CRM aswel, it is just that the redirect isn't working:它正确写入日志并且线索到达 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/>";
}

?>

I've tried just adding the header include like:我试过只添加标题包括:

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

I've looked for whitespace after "?>" and the code doesn't seem to have any errors.我在“?>”之后寻找空格,代码似乎没有任何错误。

Not sure what I'm missing..不知道我错过了什么..

For anyone wondering, I've solved it by moving the header('') redirect and all other php code to the header instead of on the bottom of the page (footer).对于任何想知道的人,我已经通过将 header('') 重定向和所有其他 php 代码移动到页眉而不是页面底部(页脚)来解决它。

Works as it should now.现在正常工作。

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

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