2015-04-22 126 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单个模型对象,将导致大量数据,因此在循环中执行它可能会导致超时。通常有一种方法只获取对象的属性。希望这可以帮助,也许这应该是在评论,但我的代表是不够的,