douya7121 2014-10-03 16:23
浏览 44

Sendgrid - PHP - 将json数据发布到我的数据库

I am trying to use the following code to connect to sendgrid with curl commands and use a foreach to post the results to my database. Right now i am getting an undefined index error on my user name and password, and also an invalid argument on my foreach. I believe i know why i am getting the foreach error, I have it commented in my code, but I am unsure about the connection aspect via the web api. Thanks

*edit - i'd just like to add that my endpoint is setup and i am able to see the test data via ngroks web interface.

<?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
    }

Still very much new to PHP so thank you for the help.

In response to the responses: is the POSTed data from sendgid in the form of an array after the json_decode function processes? I am still not quite sure what i'm doing wrong. I've amended my code to the following and i am getting an undefined index on all my variables outside of the foreach and an invalid arguement on the foreach itself...

<?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";
  • 写回答

1条回答 默认 最新

  • duancong2160 2014-10-03 20:01
    关注

    It looks like you may be misinterpreting how the Event Webhook. The webhook POSTs data to your endpoint (in the case, your PHP script) every time an event occurs (e.g. someone opens an email of yours). This all happens in real time. It is the API you'll want to use to get and store events.

    However, your script also uses App/Filter Activation Endpoint. This endpoint turns on and off the use of a Filter (also called an App). You'll only need to use this once to make sure you're posting data to your PHP Script when an event occurs. However, it may be easier just to activate it through SendGrid's Web Interface. More information about the app and the ways you can use and modify it can be found on the app description page.

    If you remove the cURL request bit from your script, you'll be left with simply getting the data from a POST request and storing it. By changing your script to the following, things should work:

    <?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"];
    }
    

    Your error was in the following line, where you tried to us the input POSTed to you as a key on the API response when you set a filter. Since it didn't exist, data was undefined and therefor not iterable with foreach.

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

    You can see a working code example of the event webhook on SendGrid's Documentation.

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害