首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Newsletter2Go REST如何向列表中添加新的收件人?

Newsletter2Go REST如何向列表中添加新的收件人?
EN

Stack Overflow用户
提问于 2018-08-01 14:13:51
回答 1查看 276关注 0票数 4

是否可以通过Newsletter2Go的REST添加一个新的收件人?

我试过这样做(片段):

代码语言:javascript
复制
public function subscribeAction()
{
    $this->init();

    $email = $this->getRequest()->getParam('email');
    $gender = $this->getRequest()->getParam('gender');
    $first_name = $this->getRequest()->getParam('first_name');
    $last_name = $this->getRequest()->getParam('last_name');

    $endpoint = "/recipients";
    $data = array(
        "list_id" => $this->listId,
        "email" => $email,
        "gender" => $gender,
        "first_name" => $first_name,
        "last_name" => $last_name,
    );

    $response = $this->curl($endpoint, $data);

    var_dump($response);
}

/**
 * @param $endpoint string the endpoint to call (see docs.newsletter2go.com)
 * @param $data array tha data to submit. In case of POST and PATCH its submitted as the body of the request. In case of GET and PATCH it is used as GET-Params. See docs.newsletter2go.com for supported parameters.
 * @param string $type GET,PATCH,POST,DELETE
 * @return \stdClass
 * @throws \Exception
 */
public function curl($endpoint, $data, $type = "GET")
{
    if (!isset($this->access_token) || strlen($this->access_token) == 0) {
        $this->getToken();
    }
    if (!isset($this->access_token) || strlen($this->access_token) == 0) {
        throw new \Exception("Authentication failed");
    }
    return $this->_curl('Bearer ' . $this->access_token, $endpoint, $data, $type);
}

private function _curl($authorization, $endpoint, $data, $type = "GET")
{
    $ch = curl_init();
    $data_string = json_encode($data);
    $get_params = "";
    if ($type == static::METHOD_POST || $type == static::METHOD_PATCH) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    if ($type == static::METHOD_POST) {
        curl_setopt($ch, CURLOPT_POST, true);
    }
    } else {
        if ($type == static::METHOD_GET || $type == static::METHOD_DELETE) {
            $get_params = "?" . http_build_query($data);
        } else {
            throw new \Exception("Invalid HTTP method: " . $type);
        }
    }
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
    curl_setopt($ch, CURLOPT_URL, static::BASE_URL . $endpoint . $get_params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: ' . $authorization,
        'Content-Length: ' . ($type == static::METHOD_GET || $type == static::METHOD_DELETE) ? 0 : strlen($data_string)
    ));
    if (!$this->sslVerification) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    }
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response);
}

https://docs.newsletter2go.com/#!/Recipient/createRecipient

但是我得到了一个大的响应对象,即使我只尝试添加一个收件人,并且新的收件人没有添加到列表中。

代码语言:javascript
复制
object(stdClass)#188 (3) {
  ["status"]=>
  int(200)
  ["info"]=>
  object(stdClass)#169 (3) {
    ["links"]=>
    object(stdClass)#43 (3) {
      ["_href"]=>
      string(60) "https://api.newsletter2go.com/recipients?_limit=50&_offset=0"
      ["_next"]=>
      string(61) "https://api.newsletter2go.com/recipients?_limit=50&_offset=50"
      ["_last"]=>
      string(63) "https://api.newsletter2go.com/recipients?_limit=50&_offset=3433"
    }
    ["count"]=>
    int(3483)
    ["additional"]=>
    object(stdClass)#167 (1) {
      ["active"]=>
      int(0)
    }
  }
  ["value"]=>
  array(50) {
    [0]=>
    object(stdClass)#85 (2) {
      ["_href"]=>
      string(49) "https://api.newsletter2go.com/recipients/n9yldrar"
      ["id"]=>
      string(8) "n9yldrar"
    }
    [1]=>
    object(stdClass)#185 (2) {
      ["_href"]=>
      string(49) "https://api.newsletter2go.com/recipients/pgwvgwmr"
      ["id"]=>
      string(8) "pgwvgwmr"
    }
              ...
    [49]=>
    object(stdClass)#87 (2) {
      ["_href"]=>
      string(49) "https://api.newsletter2go.com/recipients/usa0dx4n"
      ["id"]=>
      string(8) "usa0dx4n"
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-02 09:32:35

您必须使用POST请求来添加收件人,现在您正在发出GET请求。变化

代码语言:javascript
复制
$response = $this->curl($endpoint, $data);

代码语言:javascript
复制
$response = $this->curl($endpoint, $data, 'POST');

那就行了!

GET请求主要用于获取数据,而POST请求用于设置数据。发送的GET请求将返回所有收件人。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51635560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档