【问题标题】:Sendgrid - PHP - Posting json data to my databaseSendgrid - PHP - 将 json 数据发布到我的数据库
【发布时间】:2014-10-03 16:23:56
【问题描述】:

我正在尝试使用以下代码通过 curl 命令连接到 sendgrid,并使用 foreach 将结果发布到我的数据库。现在,我的用户名和密码出现未定义的索引错误,而且我的 foreach 参数也无效。我相信我知道为什么我会收到 foreach 错误,我在我的代码中对其进行了注释,但我不确定通过 web api 的连接方面。谢谢

*edit - 我想补充一点,我的端点已设置,我可以通过 ngroks Web 界面查看测试数据。

<?php

require_once('../functions.php');
session_start();
connect();

//Enable Connection to Event Notifications
    $url = 'https://api.sendgrid.com/';
    $user = $_SESSION["xxxxxxx"];
    $pass = $_SESSION["xxxxxxx"];
    $params = array(
        'api_user' => $user,
        'api_key' => $pass,
        'name' => 'eventnotify' //API App Name
    );
    $request = $url . 'api/filter.activate.json'; //i believe the [module] and [action] is incorrect here.

// Generate first curl request
    $session = curl_init($request);

// Tell curl to use HTTP POST
    curl_setopt($session, CURLOPT_POST, true);

// Tell curl that this is the body of the POST
    curl_setopt($session, CURLOPT_POSTFIELDS, $params);

// Tell curl not to return headers, but do return the response
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// obtain response
    $response = curl_exec($session);
    curl_close($session);


    $data = $response[file_get_contents("php://input")]; // Can i do this? Or do i just pass a $json variable?
    $records = json_decode($data, true);

    foreach ($records as $record) {
        // Here, you now have each event and can process them how you like
        echo getArray($record);
        $email = isset($record['email']) ? $record['email'] : null;
        $event = $record['event'];

        $uid = $_POST['uid'];
        $timestamp = $_POST['timestamp'];
        $event_type = $_POST['event'];
        $email_address = $_POST['email'];
        $smtpid = $_POST['smtpid'];
        $sg_event_id = $_POST['sg_event_id'];
        $sg_message_id = $_POST['sg_message_id'];
        $category = $_POST['category'];
        $newsletter = $_POST['newsletter'];
        $response = $_POST['response'];
        $reason = $_POST['reason'];
        $ip = $_POST['ip'];
        $useragent = $_POST['useragent'];
        $attempt = $_POST['attempt'];
        $status = $_POST['status'];
        $type = $_POST['type'];
        $url = $_POST['url'];
        $additional_arguments = $_POST['additional_arguements'];
        $event_post_timestamp = $_POST['event_post_timestamp'];
        $raw = $_POST['raw'];
        $customer_id = $_POST['customer_id'];

        insert("sendgrid_events", array("uid" => $uid,
            "timestamp" => $timestamp,
            "event" => $event_type,
            "email" => $email_address,
            "smtpid" => $smtpid,
            "sg_event_id" => $sg_event_id,
            "sg_message_id" => $sg_message_id,
            "category" => $category,
            "newsletter" => $newsletter,
            "response" => $response,
            "reason" => $reason,
            "ip" => $ip,
            "useragent" => $useragent,
            "attempt" => $attempt,
            "status" => $status,
            "type" => $type,
            "url" => $url,
            "additional_arguments" => $additional_arguments,
            "event_post_timestamp" => $event_post_timestamp,
            "raw" => $raw,
            "customer_id" => $customer_id)); //Unique Arguement to attach customer id on original outbound email? Yes
    }

对 PHP 还是很陌生,所以感谢您的帮助。

响应响应:在json_decode函数处理后,sendgid的POST数据是否以数组的形式?我仍然不太确定我做错了什么。我已将我的代码修改为以下内容,并且我在 foreach 之外的所有变量上都得到了一个未定义的索引,并且在 foreach 本身上得到了一个无效的争论......

<?php

require_once('../functions.php');
connect();

    $data = file_get_contents("php://input"); // Can i do this? Or do i just pass a $json variable?
    $records = json_decode($data, true);

    $uid = $_POST['uid'];
    $timestamp = $_POST['timestamp'];
    $event_type = $_POST['event'];
    $email_address = $_POST['email'];
    $smtpid = $_POST['smtpid'];
    $sg_event_id = $_POST['sg_event_id'];
    $sg_message_id = $_POST['sg_message_id'];
    $category = $_POST['category'];
    $newsletter = $_POST['newsletter'];
    $response = $_POST['response'];
    $reason = $_POST['reason'];
    $ip = $_POST['ip'];
    $useragent = $_POST['useragent'];
    $attempt = $_POST['attempt'];
    $status = $_POST['status'];
    $type = $_POST['type'];
    $url = $_POST['url'];
    $additional_arguments = $_POST['additional_arguments'];
    $event_post_timestamp = $_POST['event_post_timestamp'];
    $raw = $_POST['raw'];
    $customer_id = $_POST['customer_id'];

    foreach ($records as $record) {

        insert("sendgrid_events", array("uid" => $uid,
                                        "timestamp" => $timestamp,
                                        "event" => $event_type,
                                        "email" => $email_address,
                                        "smtpid" => $smtpid,
                                        "sg_event_id" => $sg_event_id,
                                        "sg_message_id" => $sg_message_id,
                                        "category" => $category,
                                        "newsletter" => $newsletter,
                                        "response" => $response,
                                        "reason" => $reason,
                                        "ip" => $ip,
                                        "useragent" => $useragent,
                                        "attempt" => $attempt,
                                        "status" => $status,
                                        "type" => $type,
                                        "url" => $url,
                                        "additional_arguments" => $additional_arguments,
                                        "event_post_timestamp" => $event_post_timestamp,
                                        "raw" => $raw,
                                        "customer_id" => $customer_id)); //Unique Arguement to attach customer id on original outbound email? Yes
    }

    echo "POST Received";

【问题讨论】:

  • 您是否在此脚本的开头启动 php 会话?即session_start();?
  • 很抱歉我没有包括那部分。我会编辑。

标签: php json curl sendgrid


【解决方案1】:

您似乎误解了Event Webhook 的含义。每次发生事件(例如,有人打开您的电子邮件)时,网络钩子 POSTs 将数据发送到您的端点(在本例中为您的 PHP 脚本)。这一切都是实时发生的。它您要用于获取和存储事件的 API。

但是,您的脚本也使用App/Filter Activation Endpoint。此端点打开和关闭过滤器(也称为应用程序)的使用。您只需使用一次即可确保在事件发生时将数据发布到 PHP 脚本。但是,通过SendGrid's Web Interface 激活它可能更容易。有关该应用程序以及您可以使用和修改它的方式的更多信息,请访问app description page

如果您从脚本中删除 cURL 请求位,您将只需从 POST 请求中获取数据并存储它。通过将脚本更改为以下内容,事情应该可以工作:

<?php

// All your boilerplate code
require_once('../functions.php');
connect();

// Get the raw POSTed data
$data = file_get_contents("php://input");

// Decode said data
$records = json_decode($data, true);

// Loop through the records
foreach ($records as $record) {
    // Store the event
    // Assume store_event does your data processing and inserting
    store_event($record);

    // You should be able to access any parameter of the event as so
    // $record["email"];
    // e.g.

    echo $record["email"];
    echo " had an email ";
    echo $record["event"];
}

您的错误出现在以下行中,当您设置过滤器时,您尝试将输入 POSTed 作为 API 响应的键。由于它不存在,数据是未定义的,因此不能用foreach 迭代。

// The "bad" code
$data = $response[file_get_contents("php://input")];

你可以看到working code example of the event webhook on SendGrid's Documentation

【讨论】:

  • 感谢您的洞察力,我想我现在对如何将数据发布到我的端点有了更好的理解,从而消除了对我提供的 curl 代码的需求,但我不会仍然需要在我的端点上使用我的 sendgrid 凭据进行某种身份验证?我已经修改了上面的代码,除了身份验证之外的所有内容。
  • 当您向 SendGrid 提供您希望将数据发布到的 URL 时,您已获得“授权”,因此您无需再次向 SendGrid 进行身份验证(当我们向您发布数据时) .如果您确实希望确保 SendGrid 是为您提供数据的方式,则此答案中详细说明了添加身份验证的方式:stackoverflow.com/a/20865822/648494
猜你喜欢
  • 1970-01-01
  • 2015-03-09
  • 2021-10-03
  • 1970-01-01
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-02
相关资源
最近更新 更多