[英]Parsing a large JSON file for creating wordpress posts
I have a large JSON file (>100MB) which I want to parse and then convert its objects into wordpress posts. 我有一个大的JSON文件(> 100MB),我想对其进行解析,然后将其对象转换为wordpress帖子。 I have successfully created a function to complete this task but it is not able to loop through all object, else it dies by giving an error of PHP Fatal error: 我已经成功创建了一个函数来完成此任务,但是它无法遍历所有对象,否则会因给出PHP致命错误而死:
Allowed memory size of 268435456 bytes exhausted (tried to allocate 92 bytes)
Function is able to handle files under 1MB but for files greater than 1MB it fails. 函数能够处理小于1MB的文件,但是对于大于1MB的文件,它将失败。 I have asked server administrators to increase memory limit and now no memory errors are visible but still it is not able to fetch all JSON. 我已要求服务器管理员增加内存限制,现在看不到任何内存错误,但仍然无法获取所有JSON。
I have gone through many post and questions but couldn't find/understood anything useful. 我经历了很多帖子和问题,但是找不到/理解任何有用的东西。 Also it is my first attempt to write such function, so any help/guidance will be appreciated. 这也是我第一次尝试编写此类功能,因此将不胜感激。
EDIT Added code for function used 编辑为使用的功能添加代码
function create_post_from_json($json_key) {
$json_options = get_option('json_file_data');
$obj = wp_remote_retrieve_body(wp_remote_get($json_options[$json_key]['url'],array( 'timeout' => -1 )));
$obj = json_decode($obj);
$id_stored = array();
$new_posts_id_array = array();
$new_json_posts_id_array = get_option('json_' . $json_options[$json_key]['name'] . "_post_ids");
$id_stored = get_option('json_' . $json_options[$json_key]['name']);
if(!$id_stored){$id_stored = [];}
foreach ($obj->products as $one_post) {
$post_char_id = $one_post->ID;
$new_posts_id_array[] = $post_char_id;
$cat_array = array();
if (!in_array($post_char_id, $id_stored)) {
$id_stored[] = $post_char_id;
update_option('json_' . $json_options[$json_key]['name'], $id_stored);
$post = array(
'post_title' => $one_post->name,
'post_status' => 'publish',
'post_author' => 1,
'post_content' => $one_post->description,
'post_type' => 'destinations',
);
$new_post_id = wp_insert_post($post); //post id
return true;
}
Chances are your script might be running out of time. 您的脚本可能没有时间了。
You could increase the max execution time using set_time_limit(100); // 100 seconds
您可以使用set_time_limit(100); // 100 seconds
增加最大执行时间set_time_limit(100); // 100 seconds
set_time_limit(100); // 100 seconds
Or to make it indefinite, use set_time_limit(0);
或者使用set_time_limit(0);
使其不确定set_time_limit(0);
Note: Set the time on top of your script. 注意:在脚本上方设置时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.