好的,所以根據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個電子郵件地址。
希望這會讓缺乏語境的人感覺更多一點。讓我知道如果更多的信息會使這個有用。如果能讓它對其他人更有用,我也樂意讓它更通用一些。
謝謝!
有一點要注意 - 前'session_start' using'echo'是一個沒有沒有,用()'在循環中使用'在session_start結合....... ... 不是很好。這個問題可能對所有查看都更加清楚,但是如果您將代碼完整地發佈而不是比特n件 – RamRaider
我刪除了回聲以便應該解決該問題。我還發布了完整的代碼。有什麼建議麼? – TomM0419