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

PHP使用curl将电子邮件列表发送到sparkpost API,php,curl,sparkpost,Php,Curl,Sparkpost,我想使用curl将电子邮件列表发送到SparkPostAPI 这是sparkpost文档中的卷曲 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&

我想使用curl将电子邮件列表发送到SparkPostAPI 这是sparkpost文档中的卷曲

    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实现这一点

$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);

更多信息:

如果您有权访问shell\u 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参数,它代表verbose模式,verbose模式会打印有关curl请求的额外调试信息,这会干扰输出,有关如何在命令行中使用curl的详细信息,请使用man curl。如果要在SparkPost服务器上创建电子邮件列表,可以使用以下API:以下是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中创建一个收件人列表,并在下面的请求中提供该列表的ID。您还需要一个允许REST注入的API密钥,该密钥可以在此处创建。您还需要一个有效的发送域,我假设您已经在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库:

此外,要获得更多的实时问题和答案,您可以在这里加入社区Slack:一旦您进入php频道,请查看


如果您想熟悉API,可以试用PostMan资源。这里有一篇很好的博文可以让你继续:一旦你有了这篇博文,邮递员就可以用最常用的语言编写代码来帮助你快速开始工作。

我从开始。第一个重要提示是,如果API密钥确实是这样的话,就不要共享它。我不清楚你的问题。上面的是一个metrics API调用。但你的问题似乎表明你想向列表发送电子邮件。你能澄清一下你在找什么吗?对不起,还有一个问题,关于你的问题。如果您确实想传输到列表,那么您是想在注入时提供列表,还是想使用存储在SparkPost中的列表?非常感谢,我对PHP并不陌生,我有一个从数据库发送到SparkPost的电子邮件列表,现在就这些。你的代码会派上用场的。有什么建议吗?谢谢,我有一些建议。这是您需要的API:我不久前制作了这个CLI,它允许您在此处导入收件人列表,或者您可以在WebUI中将CSV导入收件人列表。如果需要,请在PHP中执行此操作。我添加了PHP命令以将收件人列表上载到SparkPost服务器。我希望这能回答你的问题。我还更改了传输以使用该列表id