PHP函数只获取数组的最后一个值
好的,所以根据RamRaider,我已经包含了整个函数。我总是担心,当我将它变得更通用时,最终会导致变量不匹配或包含拼写错误,所以希望这些错误都不会发生。PHP函数只获取数组的最后一个值
function my_custom_popular_posts_html_list($mostpopular, $instance){
$output = 'https://startofwebaddress';
$weekday = date("N")-1;
$excerpt = '';
$popularPost = get_post($mostpopular[$weekday]->id);
$popularPostUrl = get_permalink($popularPost->ID);
$featuredImgUrl = get_the_post_thumbnail_url($popularPost->ID, 'full');
$popularPostTitle = esc_html(get_the_title($popularPost));
$popularProduct = wc_get_product($popularPost);
$popularProductLowPrice = $popularProduct->get_price();
$excerpt = get_keywords_by_id($mostpopular[$weekday]->id); //most popular = 0, 2nd most = 1, 3rd most = 2, etc.
$initial = array("oldterms");
$replace = array("newterms");
$excerpt = preg_replace($initial,$replace,$excerpt);
$commonName = str_replace("+"," ",$excerpt);
$output .= $excerpt;
$output .= 'endofwebaddress';
//Get Key for second input
$First_url = $output;
$xml = new DOMDocument();
$ch = curl_init ($First_url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//return xml
curl_setopt($ch, CURLOPT_HEADER, FALSE);//ignore
$xml->loadXML(curl_exec($ch));
curl_close ($ch);
$TagName1 = $xml->getElementsByTagName('TagName1')->item(0)->nodeValue;
//Email URL
$EmailURL = "urlthatneeds&TagName1=";
$EmailURL .= $TagName1;
$EmailURL .= "restofurl";
$text=file_get_contents($EmailURL);
$res = preg_match_all(
"/[a-z0-9]+[_a-z0-9.-]*[a-z0-9][email protected][a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})/i",
$text,
$matches
);
session_start();
foreach(array_unique($matches[0]) as $email) {
$url = 'https://api.sendgrid.com/';
$user = 'user';
$pass = 'password';
$json_string = array(
'to' => $email,
'category' => 'most_popular',
'asm_group_id' => '1141',
);
}
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => '[email protected]', //This should be ignored
'subject' => $commonName . ' Detailed Protocols and Products.',
'html' => 'A whole ton of HTML',
'text' => 'Text Version of HTML',
'from' => '[email protected]',
);
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// print everything out
print_r($response);
}
add_filter('wpp_custom_html', 'my_custom_popular_posts_html_list', 10, 2);
function get_keywords_by_id($post_id){
// Get post data
$the_post = get_post($post_id);
// Get post_content
$the_post_id = $the_post -> post_title;
return $the_post_id;
}
希望我能简化问题。 $ EmailUrl包含一个包含所有电子邮件地址的字符串。我用代码中的正则表达式解析它,以获取所有的电子邮件地址,并将其输出到数组中。
Per RichGoldMD(感谢您的建议),我在循环之外移动了session_start(),并消除了额外的变量来直接传递电子邮件。这样做时,该函数只会发送一封电子邮件给[email protected]。同样,如果我将第一个“to”属性并将其从变量更改为逗号分隔列表,我会将电子邮件发送到逗号分隔列表。然而,当我尝试输入数组变量,或者如果我尝试将数组变量拖到逗号分隔的列表中(这是我以前的样子),那么我只会收到一封电子邮件给[email protected]。如果我将第一个'to'属性更改为[$ email,'[email protected]'],我会收到一封电子邮件,发送至[email protected],并将一封电子邮件发送至数组中的最后一封电子邮件,但没有发送电子邮件到另外约1500个电子邮件地址。
希望这会让缺乏语境的人感觉更多一点。让我知道如果更多的信息会使这个有用。如果能让它对其他人更有用,我也乐意让它更通用一些。
谢谢!
好的 - 我确定了这个问题。您的foreach循环遍历您的电子邮件数组,每次替换$json_string
值,只有最后一次迭代的值传入$params
阵列。
我建议消除的foreach线和部支柱,因此更换$ json_string:
$json_string = array(
'to' => array_unique($matches[0]),
'category' => 'most_popular',
'asm_group_id' => '1141',
);
即:
$res = preg_match_all(
"/[a-z0-9]+[_a-z0-9.-]*[a-z0-9][email protected][a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})/i",
$text,
$matches
);
session_start();
// foreach(array_unique($matches[0]) as $email) { <-- Removed foreach
$url = 'https://api.sendgrid.com/';
$user = 'user';
$pass = 'password';
$json_string = array(
'to' => array_unique($matches[0]), // <-- Corrected "to"
'category' => 'most_popular',
'asm_group_id' => '1141',
);
// } <-- Removed brace
$params = array(....
---------前面的答案如下 - ----
绝对将session_start移出循环(这不是问题,但它不属于循环中的任何情况)。
如果api需要一个数组,请使用implode放置该行,并直接传递$ email而不是将其按摩到$ emailAddresses中。
implode语句产生一个字符串,但是您的注释表明数组是预期的。
foreach(array_unique($matches[0]) as $email) {
$url = 'https://api.sendgrid.com/';
$user = 'User';
$pass = 'Password';
$json_string = array(
'to' => $email,
'category' => 'my_category',
'asm_group_id' => '1234',
);
...
@ TomM0419 $ matches [0]的值是多少?这是你期望的吗?您可以使用error_log(print_r($ matches [0],true))将它回显或转储到错误日志中; – RichGoldMD
我也想知道,如果发送网格在单个命令中有目的地数量的限制 - 你检查了发送网格日志吗? – RichGoldMD
太棒了。这让我很直,并且非常接近。如果unique_array($匹配[0]),那么函数返回一个空数组,默认为[email protected]。如果我编辑'to'等于''到'='匹配[0],然后在$ params中将'x-smtpapi'更改为''x-smtpapi'=> json_encode(array_unique($ json_string)),那么它的作品!谢谢您的帮助。附注:我找到了您的链接cancerexpertnow.com。我有一个类似的兴趣与不同的倾向(博士的观点赞美MD透视),我想聊聊你是否有时间。你能发送联系信息吗? – TomM0419
有一点要注意 - 前'session_start' using'echo'是一个没有没有,用()'在循环中使用'在session_start结合....... ... 不是很好。这个问题可能对所有查看都更加清楚,但是如果您将代码完整地发布而不是比特n件 – RamRaider
我删除了回声以便应该解决该问题。我还发布了完整的代码。有什么建议么? – TomM0419