如何处理序列化 - Campaign Monitor API
问题描述:
我正在使用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
,一切正常,但是当我添加一些参数,它不会工作
答
天儿真好user1611830 ,
根据sample in the github repo,单线,多线和图像中的每一个都必须是array
的arrays
。
而且最好为中继器包含一个空的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()
);
看起来这个请求应该是json编码的。你有没有试过在将数组传递给create_from_template方法之前,在json_encode()中包装数组? –
@ChrisHanson,看到我的编辑,它似乎可以理解数组格式 – user1611830
@ChrisHanson :事实上,我设法使用'TemplateContent'条目嵌套数组=>数组(数组('Content'=> $ content,..)), – user1611830