【问题标题】:create a sendgrid list & campaign in php在 php 中创建 sendgrid 列表和活动
【发布时间】:2016-01-13 19:05:15
【问题描述】:

我尝试使用 api php 将电子邮件添加到我的 sendgrid 帐户中的现有联系人列表,但我的代码出现问题,返回以下消息:

{"traceback": "'Traceback (last last call last):\nFailure: exceptions.ValueError: Test does not exist\n', "error": "Test doesn't exist"}

对于创建活动按钮,它返回此错误:

{"errors":[{"field":null,"message":"需要授权"}]}

那么,你知道如何在 php 中创建这个活动吗? 这是我正在尝试的:

<html>
<head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <title>Ajax - La fonction ajax()</title>
</head>
<body>
<?php
    $url = 'https://api.sendgrid.com/';
    $user = '******';
    $pass = '********';

    $params = array(
        'api_user' => $user,
        'api_key' => $pass,
        'to' => 'hjhgfr3@gmail.com',
        'subject' => 'Hello Girl !',
        'html' => 'This is me ! :D',
        'text' => 'Hello, Girl',
        'from' => 'myemail@mydomain.com',
    );

    $request = $url.'api/mail.send.json';

    // Generate curl request
    $session = curl_init($request);
?>
<button id="action">add to list</button><br /><br>
<button id="action2">Create campaign </button>

<script>
    $(function() {
        $('#action').click(function() {
            $.ajax({
                type: 'GET',
                url: 'https://api.sendgrid.com/api/newsletter/lists/email/get.json?api_user=******&api_key=********&list=Test&email@email.com',
                /* timeout: 3000,*/
                success: function(data) {
                    alert(data); },
                error: function() {
                    alert('La requête n\'a pas abouti');
                }
            });
        });
    });
</script>

<script> 
    $("#action2").click(function() {
        $.get("https://api.sendgrid.com/v3/campaigns?Authorization=Bearer+API-KEY",
            {
                "title": "March Newsletter",
                "subject": "New Products for Spring!",
                "sender_id": 124451,
                "list_ids": [
                    110,
                    124
                ],
                "segment_ids": [
                    110
                ],
                "categories": [
                    "spring line"
                ],
                "suppression_group_id": 42,
                "custom_unsubscribe_url": "",
                "ip_pool": "marketing",
                "html_content": "<html><head><title></title></head><body><p>Check out our spring line!</p></body></html>",
                "plain_content": "Check out our spring line!"
            },
            function(data, status){
                alert("Data: " + data + "\nStatus: " + status);

            });
    });
</script>

【问题讨论】:

标签: php api post curl sendgrid


【解决方案1】:

看起来有几个问题。

  1. 您没有正确授权。您需要在您的 api 请求中设置 Authorization 标头。 查看文档了解更多信息: https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html

  2. 我看到您正在发出 GET 请求,在这种情况下,GET 只会检索数据,而不会让您创建活动或添加到列表中。您需要使用文档中提到的 POST,https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html

另外,使用 PHP 的 HTTP 客户端库(例如 HTTPFUL)而不是使用 Curl 可能会有所帮助,但无论您喜欢什么。

【讨论】:

  • 谢谢!现在已修复
猜你喜欢
  • 1970-01-01
  • 2017-04-26
  • 2016-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
相关资源
最近更新 更多