2017-07-17 156 views
-2

好的,所以根據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個電子郵件地址。

希望這會讓缺乏語境的人感覺更多一點。讓我知道如果更多的信息會使這個有用。如果能讓它對其他人更有用,我也樂意讓它更通用一些。

謝謝!

+1

有一點要注意 - 前'session_start' using'echo'是一個沒有沒有,用()'在循環中使用'在session_start結合....... ... 不是很好。這個問題可能對所有查看都更加清楚,但是如果您將代碼完整地發佈而不是比特n件 – RamRaider

+0

我刪除了回聲以便應該解決該問題。我還發布了完整的代碼。有什麼建議麼? – TomM0419

回答

0

好的 - 我確定了這個問題。您的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', 
); 

...

+0

@ TomM0419 $ matches [0]的值是多少?這是你期望的嗎?您可以使用error_log(print_r($ matches [0],true))將它回顯或轉儲到錯誤日誌中; – RichGoldMD

+0

我也想知道,如果發送網格在單個命令中有目的地數量的限制 - 你檢查了發送網格日誌嗎? – RichGoldMD

+0

太棒了。這讓我很直,並且非常接近。如果unique_array($匹配[0]),那麼函數返回一個空數組,默認爲[email protected]。如果我編輯'to'等於''到'='匹配[0],然後在$ params中將'x-smtpapi'更改爲''x-smtpapi'=> json_encode(array_unique($ json_string)),那麼它的作品!謝謝您的幫助。附註:我找到了您的鏈接cancerexpertnow.com。我有一個類似的興趣與不同的傾向(博士的觀點讚美MD透視),我想聊聊你是否有時間。你能發送聯繫信息嗎? – TomM0419