PHP使用curl将电子邮件列表发送到sparkpost API

时间:2018-01-04 15:09:08

标签: php curl sparkpost

我想使用curl向sparkpost API发送电子邮件列表 这里是sparkpost docs中的卷曲

    curl -v \
-H "Content-Type: application/json" \
-H "Authorization: $API_KEY" \
-X GET "https://api.sparkpost.com/api/v1/metrics/deliverability/aggregate?campaigns=testjob&from=2014-01-23T14:00&metrics=count_targeted,count_sent,count_accepted&timezone=America%2FNew_York&to=2014-06-23T15:50"

如何使用PHP curl

执行此操作

3 个答案:

答案 0 :(得分:1)

如果要在SparkPost服务器上创建电子邮件列表,可以使用此API:https://developers.sparkpost.com/api/recipient-lists.html#recipient-lists-create-post以下是PHP中的示例:

<?php

$request = new HttpRequest();
$request->setUrl('https://api.sparkpost.com/api/v1/recipient-lists');
$request->setMethod(HTTP_METH_POST);

$request->setQueryData(array(
  'num_rcpt_errors' => '3'
));

$request->setHeaders(array(
  'Cache-Control' => 'no-cache',
  'Authorization' => $YOUR_API_KEY_HERE // put your API key here
));

// You will need to put PHP code that reads from your DB and puts Your recipients in that array below. You only need the address->name and address-email bit if you don't have additional metadata
$request->setBody('{
  "id": "unique_id_4_graduate_students_list",
  "name": "graduate_students",
  "description": "An email list of graduate students at UMBC",
  "attributes": {
    "internal_id": 112,
    "list_group_id": 12321
  },
  "recipients": [
    {
      "address": {
        "email": "wilmaflin@example.com",
        "name": "Wilma"
      },
      "tags": [
        "greeting",
        "prehistoric",
        "fred",
        "flintstone"
      ],
      "metadata": {
        "age": "24",
        "place": "Bedrock"
      },
      "substitution_data": {
        "favorite_color": "SparkPost Orange",
        "job": "Software Engineer"
      }
    },
    {
      "address": {
        "email": "abc@example.com",
        "name": "ABC"
      },
      "tags": [
        "driver",
        "flintstone"
      ],
      "metadata": {
        "age": "52",
        "place": "MD"
      },
      "substitution_data": {
        "favorite_color": "Sky Blue",
        "job": "Driver"
      }
    },
    {
      "address": {
        "email": "fred.jones@example.com",
        "name": "Grad Student Office",
        "header_to": "grad-student-office@example.com"
      },
      "tags": [
        "driver",
        "fred",
        "flintstone"
      ],
      "metadata": {
        "age": "33",
        "place": "NY"
      },
      "substitution_data": {
        "favorite_color": "Bright Green",
        "job": "Firefighter"
      }
    }
  ]
}');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

假设您要将电子邮件发送到下面存储在SparkPost中的收件人列表,这是在PHP中执行此操作的一种方法。

为此,您需要在SparkPost中创建一个“收件人列表”https://app.sparkpost.com/lists/recipients,并在下面的请求中提供列表的ID。您还需要一个允许在此处创建的REST注入的API密钥https://app.sparkpost.com/account/credentials您还需要一个有效的发送域,我假设您已经在SparkPost中进行了设置。

<?php

$request = new HttpRequest();
$request->setUrl('https://api.sparkpost.com/api/v1/transmissions');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'Cache-Control' => 'no-cache',
  'Authorization' => $YOUR_SPARKPOST_API_KEY . // Put your "real" API key here or better set a variable and use that
));

$request->setBody('{
    "options": {
        "open_tracking": true,
        "click_tracking": true
    },
  "campaign_id": "test_campaign",
  "recipients": {
    "list_id": "unique_id_4_graduate_students_list" // Put your List ID here from here https://app.sparkpost.com/lists/recipients
  },
  "content": {
    "from": {
      "email": "test@test.example.com", // Change this to your proper from address
      "name": "John Doe"
    },
    "subject": "simple subject",
    "text": "simple text content",
    "html": "<b>Your HTML Content</b>"
  }
}

');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

以下是一些可能让您更快前进的提示。这里有一个SparkPost PHP库:https://github.com/SparkPost/php-sparkpost

此外,对于更多“实时”问题和答案,您可以加入社区Slack:http://slack.sparkpost.com/一旦您查看了#php频道。

如果您想熟悉API,可以试用PostMan资源。这是一篇很好的博客文章,可以帮助您:https://www.sparkpost.com/blog/how-to-run-sparkpost-postman/一旦您完成了这项工作,PostMan可以使用大多数常用语言生成代码,以帮助您快速上手。

答案 1 :(得分:0)

$ch = curl_init("https://api.sparkpost.com/api/v1/metrics/deliverability/aggregate?campaigns=testjob&from=2014-01-23T14:00&metrics=count_targeted,count_sent,count_accepted&timezone=America%2FNew_York&to=2014-06-23T15:50");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: $API_KEY',
'Content-Type: application/json'
));

$data = curl_exec($ch);
curl_close($ch);

更多信息:Docs

答案 2 :(得分:0)

如果您有权访问shell_exec(),则可以将其作为shell命令行调用:

<?php
$output = shell_exec('curl -H "Content-Type: application/json" -H "Authorization: '.$API_KEY.'" -X GET "https://api.sparkpost.com/api/v1/metrics/deliverability/aggregate?campaigns=testjob&from=2014-01-23T14:00&metrics=count_targeted,count_sent,count_accepted&timezone=America%2FNew_York&to=2014-06-23T15:50"');

echo $output;
?>

假设您只对服务器响应感兴趣,我从命令行中删除了-v参数,该命令行代表详细模式,详细模式打印有关curl请求的额外调试信息,这将导致输出混乱,有关如何在命令行中使用curl的更多信息,请使用man curl