如何将POST数据发送到EmailOctopus API

时间:2019-05-22 18:32:01

标签: php api codeigniter post

我正在开发一个项目,并且试图集成EmailOctopus的API。无论出于何种原因,我都无法使POST请求生效。我正在使用Codeigniter来构建我的项目。

我已经尝试过许多在stackoverflow上发现的不同方法,但是通过这种方法,我至少会收到一条错误消息。

电子邮件章鱼文档 https://emailoctopus.com/api-documentation/lists/create-contact

    public function form_handler($funnel_id, $page_id){

    //get the list_id
    $list_id = $this->Pages_Model->get_list_id($page_id)->list_id;

    //get the api_key
    $api_key = $this->Admin_Model->get_api_key()->api_key;


    $url = "https://emailoctopus.com/api/1.5/lists/" . $list_id . 
    "/contacts";


    $data = '{"api_key": "' . trim($api_key) . '",';


    foreach($_POST as $key => $value){
      if($key == "emailAddress"){
        $data .= '"email_address": "' . $value . '",';
      }
    }
    $data .= '"fields": {';
    foreach($_POST as $key => $value){
      if($key != "emailAddress"){
        $data .= '"' . $key . '": "' . $value . '",';
      }
    }
    $data .= '},';
    $data .= '"status": "SUBSCRIBED"}';



    $options = array(
      'http' => array(
        'header' => "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n",
        'method' => 'POST',
        'content' => json_encode($data)
      )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    if($result === FALSE){

    }

    $response = json_decode($result);
    var_dump($response);

  }




It's telling me my api_key is invalid but it's not.

//THIS IS THE OUTPUT/RETURN I GET FROM THE REQUEST:

   C:\wamp64\www\the-funnel- 
   blaster\application\controllers\Form_controller.php:66:
   object(stdClass)[25]
   public 'error' => 
   object(stdClass)[26]
   public 'code' => string 'API_KEY_INVALID' (length=15)
   public 'message' => string 'Your API key is invalid.' (length=24)
   public '0' => int 403

0 个答案:

没有答案