【问题标题】:Stop wordpress PHP curl requests from happening twice阻止 wordpress PHP curl 请求发生两次
【发布时间】:2015-11-19 17:34:04
【问题描述】:

我正在尝试在我的网站上使用 Customer.io 跟踪用户活动。我正在发送带有用户信息、页面浏览量和触发事件(购买)的 curl 请求。由于某种原因,curl 请求被发送了两次。

我的 footer.php 文件中有以下代码。 (购买 curl req 未在其他文件中显示)。

<?php
$current_user = wp_get_current_user();

if ($current_user instanceof WP_User) {

    $userId = $current_user->ID;
    $userEmail = $current_user->user_email;
    $userRegisterDate = strtotime($current_user->user_registered);
    $userFirstName = $current_user->user_firstname;

    if (($userId && $userEmail && $userRegisterDate && $userFirstName)) {
        $session = curl_init();

        $customer_id = $userId; 
        $customerio_url = 'https://track.customer.io/api/v1/customers/';
        $site_id = 'xxxxxxxx';
        $api_key = 'xxxxxxxx';

        $data = array('email' => $userEmail, 'first_name' => $userFirstName, 'created_at' => $userRegisterDate);

        curl_setopt($session, CURLOPT_URL, $customerio_url . $customer_id);
        curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($session, CURLOPT_HTTPGET, 1);
        curl_setopt($session, CURLOPT_HEADER, false);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($session, CURLOPT_VERBOSE, 1);
        curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($session, CURLOPT_USERPWD, $site_id . ':' . $api_key);

        curl_exec($session);
        curl_close($session);

        $session = curl_init();
        $customerio_url = 'https://track.customer.io/api/v1/customers/' . $customer_id . '/events';
        $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

        $data = array('name' => $actual_link, 'type' => 'page', 'data[referrer]' => 'http://www.spanishvirtually.com');

        curl_setopt($session, CURLOPT_URL, $customerio_url);
        curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($session, CURLOPT_HEADER, false);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($session, CURLOPT_VERBOSE, 1);
        curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($session, CURLOPT_USERPWD, $site_id . ':' . $api_key);

        curl_exec($session);
        curl_close($session);
    }
}
?>

当我在 customer.io 中检查我的活动日志时,所有页面事件视图都是重复的。似乎 curl 请求被发送了两次。我假设 footer.php 页面正在加载/被调用两次。

我怎样才能防止这种情况发生?

我在自定义 woocommerce 模板文件中也有类似的代码(theme/woocommerce/checkout/thankyou.php,它也发送了两次 curl 请求)

【问题讨论】:

  • 我假设你调用了这个页面两次。尝试在此页面顶部放置一个邮件声明,然后调用它,看看你是否收到两封邮件
  • 是的。我收到了两封电子邮件
  • 所以你需要更上一层楼并弄清楚这个页面是从哪里包含的,但我们无法通过查看这个页面来弄清楚:)
  • 这是我主题根目录下的footer.php
  • 您需要在每次页面加载时运行它吗?或者你能把它附加到一个特定的do_action 钩子上吗?尽管如果您的页面加载了两次,那可能无法解决任何问题。我建议在另一个主题中进行测试和/或禁用其他插件,看看你是否能找到根本原因。

标签: php wordpress curl woocommerce


【解决方案1】:

这不是标准行为,但您可以通过将代码包装在语句中以查看它是否已运行来阻止它。

if (!defined('TRACK_CUSTOMER')) {
    define('TRACK_CUSTOMER', 1);

    $current_user = wp_get_current_user();

    if ($current_user instanceof WP_User) {
        // ...
}

【讨论】:

  • 他的双页加载问题的一个很好的解决方法。
  • 似乎这样可以,但它仍然会加载两次if (!defined('TRACK_CUSTOMER')) { define('TRACK_CUSTOMER', 1); mail("myemail@example.com", "test", "only once please"); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 2022-11-29
相关资源
最近更新 更多