curlopt_postfields $ params'to'作为数组? [英] curlopt_postfields $params 'to' as an array?
问题描述
与这里的完全相同的方法: SendGrid与动态PHP参数,但我' m尝试做是设置多个'到'和'cc'电子邮件地址。可以将它们设置为我当前实现的数组吗
Exact same approach as here: SendGrid with dynamic PHP params but what I'm trying to do is set multiple 'to' and 'cc' email addresses. Can these be set as an array with my current implementation?
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => array('user1@example.com','user2@example.com'),
'cc' => array('user3@example.com','user4@example.com'),
'subject' => 'test email',
'html' => '<p>This is a test email</p>',
'text' => 'done',
'from' => 'admin@example.com'
);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params;
并且当我只在to和cc中设置单个电子邮件而没有数组时发送电子邮件,但是当我添加为数组时,它不会运行。
It works well and sends out the email when I only set a single email in the 'to' and 'cc' without an array, but when I add as an array it won't run.
我也试过了,但它不运行。
I also tried this but it doesn't run.
curl_setopt ($session, CURLOPT_POSTFIELDS, http_build_query($params);
不幸的是,文档中没有解释如何传递到/ cc字段中的多个地址 - a href =https://sendgrid.com/docs/Code_Examples/php.html =nofollow> https://sendgrid.com/docs/Code_Examples/php.html 。
Unfortunately nothing in the documentation explains how to pass in multiple addresses in the to/cc fields - https://sendgrid.com/docs/Code_Examples/php.html.
推荐答案
PHP的cURL库只知道如何处理一维数组,所以你需要手工构建数据字符串使用 http_build_query()
,然后设置 CURLOPT_POSTFIELDS
。
PHP's cURL library only knows how to deal with one-dimensional arrays, so you need to build the data string manually using http_build_query()
and then set that for CURLOPT_POSTFIELDS
.
如果您需要更好地控制数据字符串的格式化[例如:文件上传在复杂的帖子]下面的一个应该工作。
If you need greater control over the formatting of the data string [eg: file uploads in a complex post] one of the below should work.
$params = array(
'api_user' => 'foo',
'api_key' => 'bar',
'to' => array('user1@example.com','user2@example.com'),
'cc' => array('user3@example.com','user4@example.com'),
'subject' => 'test email',
'html' => '<p>This is a test email</p>',
'text' => 'done',
'from' => 'admin@example.com',
'file' => '@foobar.txt'
);
// returns a query string
function encode1($params) {
$data_arr = array();
foreach( $params as $key => $value ){
if( ! is_array($value) ) {
if( strpos($value, '@') === 0 ) { $encvalue = $value; }
else { $encvalue = urlencode($value); }
$data_arr[] = sprintf('%s=%s', urlencode($key), $encvalue);
} else {
foreach( $value as $item ) {
if( strpos($item, '@') === 0 ) { $encvalue = $item; }
else { $encvalue = urlencode($item); }
$data_arr[] = sprintf('%s=%s', urlencode($key.'[]'), $encvalue);
}
}
}
return implode('&', $data_arr);
}
var_dump(encode1($params));
/* result:
string(254) "api_user=foo&api_key=bar&to%5B%5D=user1%40example.com&to%5B%5D=user2%40example.com&cc%5B%5D=user3%40example.com&cc%5B%5D=user4%40example.com&subject=test+email&html=%3Cp%3EThis+is+a+test+email%3C%2Fp%3E&text=done&from=admin%40example.com&file=@foobar.txt"
*/
// returns an array
function encode2($params) {
$out = array();
foreach( $params as $key => $value ) {
if( ! is_array($value) ) {
$out[$key] = $value;
} else {
for( $i=0; $i<count($value); $i++ ) {
$index = sprintf('%s[%d]', $key, $i);
$out[$index] = $value[$i];
}
}
}
return $out;
}
var_dump(encode2($params));
/* result:
array(11) {
["api_user"]=>string(3) "foo"
["api_key"]=> string(3) "bar"
["to[0]"]=> string(17) "user1@example.com"
["to[1]"]=> string(17) "user2@example.com"
["cc[0]"]=> string(17) "user3@example.com"
["cc[1]"]=> string(17) "user4@example.com"
["subject"]=> string(10) "test email"
["html"]=> string(27) "<p>This is a test email</p>"
["text"]=> string(4) "done"
["from"]=> string(17) "admin@example.com"
["file"]=> string(11) "@foobar.txt"
}
*/
这篇关于curlopt_postfields $ params'to'作为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!