2015-04-22 46 views
0

有兩個功能,一個是init功能,從數據庫中獲得一些信息,然後將帳戶信息轉移到另一個功能從遠程網站上的數據,然後test_function_save_data()得到正確的數據,並檢查&插入它們進入數據庫。PHP的foreach多個陣列超時

問題是:如果在test_function_save_data()中使用print_r($post),該功能可以正常工作。但刪除此print_r或將其替換爲sleep(),可以檢查所有數據並將其插入到數據庫中,但當前頁面未重定向,將顯示空白頁面。

那麼,哪裏出錯?如何解決它?

<?php 
function test_function_get_account_init() { 
    if(test_function_get_sns_account('blog')) { 
     $b = 0; 
     foreach (test_function_get_sns_account('blog') as $team_id => $blog_account_name) { 
      $blog = test_theme_get_blog($team_id, $blog_account_name); 
      if($blog && is_array($blog['posts'])){ 
       $b += test_function_save_data($blog['posts']); 
      } 
     } 
     echo '<h5successfuly!</h5>'; 
    } else { 
     echo 'not found.'; 
    } 
} 

function test_function_save_data($post_data) { 
    set_time_limit(720); 
    global $wpdb; 
    $i = 0; 
    foreach ($post_data as $post) { 
     // echo '<div style="display:none;">'; 
     // print_r($post); 
     // echo '</div>'; 
     if(test_function_check_post_unquine($post['social_origin_id']) && isset($post['social_origin_id'])) { 
      $social_post_data = array(
       'post_title' => $post['post_title'], 
       'post_content' => $post['post_content'], 
       'post_status' => 'publish', 
       'post_author' => get_current_user_id(), 
       'post_type'  => 'social_post' 
      ); 
      $latest_post_id = wp_insert_post($social_post_data); 
      if($post['social_origin_url']) { 
       add_post_meta($latest_post_id, 'social_origin_url', $post['social_origin_url']); 
      } 
      if($post['social_origin_id']) { 
       add_post_meta($latest_post_id, 'social_origin_id', $post['social_origin_id']); 
      } 
      if($post['user']) { 
       add_post_meta($latest_post_id, 'social_origin_user', $post['user']); 
      } 
      if($post['user']['team_account']) { 
       add_post_meta($latest_post_id, $post['user']['site_name'].'_social_user_id', $post['user']['team_account']); 
      } 
      if($post['media']) { 
       add_post_meta($latest_post_id, 'social_origin_thumbnail', $post['media']['url']); 
      } 
      $i++; 
     } 
     flush(); 
    } 
    return $i; 
} 
+0

你可以請格式化您的代碼? –

+0

謝謝。現在格式化。 –

回答

0

這看起來像一個wordpress代碼?您是否測試了print_r單個$ post?在一些框架中,大多數對象都是非常複雜的類的實例。例如。在laravel中,如果嘗試print_r單個模型對象,將導致大量數據,因此在循環中執行它可能會導致超時。通常有一種方法只獲取對象的屬性。希望這可以幫助,也許這應該是在評論,但我的代表是不夠的,