2013-02-04 51 views
0

我正在使用Campaign Monitor API發送電子郵件。 API允許通過名爲create_from_template()的方法創建模板化電子郵件。由於the doc says,我需要添加一些格式化的參數。下面是活動監視器create_from_template()方法實現的:如何處理序列化 - Campaign Monitor API

/** 
    * Creates a new campaign from a template based on the info provided. 
    * At least on of the ListIDs and Segments parameters must be provided 
    * @param string $client_id The client to create the campaign for 
    * @param array $campaign_info The campaign information to use during creation. 
    *  This array should be of the form 
    *  array(
    *   'Subject' => string required The campaign subject 
    *   'Name' => string required The campaign name 
    *   'FromName' => string required The From name for the campaign 
    *   'FromEmail' => string required The From address for the campaign 
    *   'ReplyTo' => string required The Reply-To address for the campaign 
    *   'ListIDs' => array<string> optional An array of list ids to send the campaign to 
    *   'SegmentIDs' => array<string> optional An array of segment ids to send the campaign to 
    *   'TemplateID' => string required The ID of the template to use 
    *   'TemplateContent' => array required The content which will be used to fill the editable areas of the template 
    * ) 
    * @access public 
    * @return CS_REST_Wrapper_Result A successful response will be the ID of the newly created campaign 
    */ 
    function create_from_template($client_id, $campaign_info) 
    { 
     return $this->post_request($this->_base_route . 'campaigns/' . $client_id . '/fromtemplate.json', $campaign_info); 
    } 

因此我給了爲$ campaign_info參數

 $list = array($list_id); 
     $data = array(
      'Name' => $name_string, 
      'Subject' => $subject_string, 
      'FromName' => $some_name, 
      'FromEmail' => "[email protected]", 
      'ReplyTo' => "[email protected]", 
      'ListIDs' => $list_id, 
      'SegmentIDs' => array(), 
      'TemplateID' => 'a6dd1168417a6d7d7f94da70c3cafe15' 
      'TemplateContent' => array(
             'Singlelines' => array('Content' => $a_string , 
                   'Href' => $a_href 
                  ), 
             'Multilines' => array('Content' => $content 
                  ), 
             'Images' => array('Content' => $some_url, 
                  'Href' => $some_href, 
                 ) 

            ) 
       ); 

但發動我的API POST請求時,廣告活動的監視服務器,我一直有一個消息:

object(CS_REST_Wrapper_Result)#32 (2) { ["response"]=> object(stdClass)#29 (2) { ["Code"]=> int(400) ["Message"]=> string(110) "Failed to deserialize your request. Please check the documentation and try again. Fields in error: campaign" } ["http_status_code"]=> int(400) } 

有沒有人知道它可能來自哪裏?

編輯:如果我把這些參數傳遞

$template_content = array(
    'Singlelines' => array(), 
    'Multilines' => array(), 
    'Images' => array(), 
    'Repeaters' => array() 
); 

,然後在陣列中$data'TemplateContent' => $template_content,一切正常,但是當我添加一些參數,它不會工作

+0

看起來這個請求應該是json編碼的。你有沒有試過在將數組傳遞給create_from_template方法之前,在json_encode()中包裝數組? –

+0

@ChrisHanson,看到我的編輯,它似乎可以理解數組格式 – user1611830

+0

@ChrisHanson :事實上,我設法使用'TemplateContent'條目嵌套數組=>數組(數組('Content'=> $ content,..)), – user1611830

回答

3

天兒真好user1611830 ,

根據sample in the github repo,單線,多線和圖像中的每一個都必須是arrayarrays

而且最好爲中繼器包含一個空的array

所以你可能在尋找這樣的事情(道歉,如果我錯過任何括號,但只要你包裝每個Singlelines,多線和圖像的另一array()你應該是正確的:

$template_content = array(
    'Singlelines' => array(
     array(
      'Content' => $a_string, 
      'Href' => $a_href 
     ) 
    ), 
    'Multilines' => array(
     array('Content' => $content) 
    ), 
    'Images' => array(
     array(
      'Content' => $some_url, 
      'Href' => $some_href, 
     ) 
    ), 
    'Repeaters' => array() 
); 
相關問題