执行php函数点击html 204 returnneed

时间:2017-08-25 23:52:07

标签: php html amazon-web-services heroku hubspot

我正在尝试将下面的html表单数据发布到hubspot但是当我将它部署到heroku或aws弹性beanstalk时,我收到204返回而不是我的数据被发布到crm。我使用了how to execute php function on html button click

中找到的解决方案

但是没有成功获取hubspot所需的信息。

html file   
 <!DOCTYPE html>
    <html lang="en">
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script type="text/javascript">
            function myAjax () {
                $.ajax( { type : 'POST',
                    data : { },
                    url  : 'index.php',              // <=== CALL THE PHP FUNCTION HERE.
                    success: function ( data ) {
                        alert( data );               // <=== VALUE RETURNED FROM FUNCTION.
                    },
                    error: function ( xhr ) {
                        alert( "error" );
                    }
                });
            }
        </script>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form>
        <div class="row">
            <div class="row">
                <div class="col-lg-12 form-group">
                    <label for="email">Email</label>
                    <input
                            type="email"
                            id="email"
                            class="form-control"
                            name="email"
                            required
                            pattern="'^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$'"
                    >
                </div>
                <div class="row moveRight">
                    <div class="col-lg-12 form-group">
                        <label for="firstname">First Name</label>
                        <input
                                type="text"
                                id="firstname"
                                class="form-control"
                                name="firstname"
                                required
                        >
                    </div>
                </div>
                <div class="row moveRight">
                    <div class="col-lg-12 form-group">
                        <label for="lastname">Last Name</label>
                        <input
                                type="text"
                                id="lastname"
                                class="form-control"
                                name="lastname"
                                required
                        >
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <button onclick="myAjax()" class="btn btn-success" id="startnode" type="submit">Submit</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include 'index.php';?>
    </body>
    </html>

php文件

<?php

function bb() {
   //Process a new form submission in HubSpot in order to create a new Contact.

$hubspotutk      = $_COOKIE['hubspotutk']; //grab the cookie from the visitors browser.
$ip_addr         = $_SERVER['REMOTE_ADDR']; //IP address too.
$hs_context      = array(
    'hutk' => $hubspotutk,
    'ipAddress' => $ip_addr,
    'pageUrl' => 'http://www.example.com/form-page',
    'pageName' => 'Example Title'
);
$hs_context_json = json_encode($hs_context);

//Need to populate these variable with values from the form.
$str_post = "firstname=" . urlencode($firstname)
    . "&lastname=" . urlencode($lastname)
    . "&email=" . urlencode($email)
    . "&hs_context=" . urlencode($hs_context_json); //Leave this one be

//replace the values in this URL with your portal ID and your form GUID
$endpoint = 'https://forms.hubspot.com/uploads/form/v2/hubid/guid';

$ch = @curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post);
@curl_setopt($ch, CURLOPT_URL, $endpoint);
@curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded'
));
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response    = @curl_exec($ch); //Log the response from HubSpot as needed.
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
@curl_close($ch);
echo $status_code . " " . $response;
}

bb();

?>

0 个答案:

没有答案