在调用 wp_update_post($ my_post)之后,我检查了memory_get_usage()。在foreach循环内的每次调用更新后,内存使用量越来越高
以下是来自 echo memory_get_usage(); 的输出,我在wp_update_post()后执行了这些输出:
第一个电话= 72923956
第二个电话= 89410604
第3个电话= 105152440
第4个电话= 120900116
第5个电话= 136651592
第6个电话= 152432716
第7次电话= 168178896
然后......
致命错误:第1196行中的... / wp-includes / wp-db.php中允许的内存大小为268435456字节(试图分配15728640字节)
我的问题是:
是否有可能刷新内存,删除我为 wp_update_post()构建的$ my_post数组的内容?顺便说一句:数组的大小是每个大约1000个字符...
这是一个通用代码。您可以搜索“wp_update_post”以查找存在内存问题的行
Thanx帮忙,
垫
<?
function event_sync_doit() {
$limit = $_POST['limit']; // 1-1500
$start = $_POST['start']; // 0-1500
$range = $_POST['range']; // 1-100
$end = min ( $start + $range , $limit );
$string = file_get_contents("https://www.example.com/party/api/json/?limit=".$limit);
$json=json_decode($string,true);
for ($i = $start; $i <= $end; $i++) {
$event_id = $json['partylist'][$i][id];
// ######### DELETED PART #########
// Here we have a lot of variable assignments
// ######### DELETED PART #########
if ( ($event_id>0) ) {
// READ XML FROM example.com API
$xml = simplexml_load_file('https://www.example.com/party/xml/'.$event_id); // or exit();
// ######### DELETED PART #########
// Here we have a lot of variable assignments
// ######### DELETED PART #########
// SET FLYER
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$flyerfull_url = $xml->party[0]->{'flyerfull-url'};
$flyersmall_url = $xml->party[0]->{'flyersmall-url'};
$flyer_title = $start_date." - ".$partyname;
if ( ($post_thumbnail_id > 0) AND ($post_thumbnail_id != 14072)) {
// there is already a a standard post thumbnail with id 14072
} else if ($flyerfull_url != "") {
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($flyerfull_url);
$filename = basename($flyerfull_url);
if(wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
}
else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => $flyer_title,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
} else {
// set placeholder picture
set_post_thumbnail( $post_id, "14072" );
}
// WRITE CONTENT HTML
if ( intval($lat) > 0 AND intval($lon) > 0 ) {
$map_info = "<br><br><i>Scroll down for location map</i>";
}
else {
$map_info = "";
}
// ######### DELETED PART #########
$out = "COMPLETE STRING OF CONTENT - APPX 1000-2000 CHARACTERS EACH ENTRY";
// ######### DELETED PART #########
// -----------------
// LINK URLS
// -----------------
$out = preg_replace_callback(
'/((https?:\/\/)(www\.)|(https?:\/\/)|(www\.))([^<\/\?\s]+)[^<\s]*/',
function ($matches) {
switch($matches[6]){
// ------------------------------------------------------------------------
case 'facebook.com':
$graph_url = str_replace("//www.","//graph.",url_trim($matches[0],"http://"));
$alter_error_handler = set_error_handler("mush_ErrorHandler");
$seite = file_get_contents($graph_url, false, $context);
restore_error_handler();
$graph = json_decode($seite, true);
if ($graph["id"] > 0 AND $graph["gender"] = "") {
$return = "<br>[dpSocialTimeline skin=\"light\" total=\"2\" items=\"{'name':'facebook_page','data':'".$graph["id"]."','limit':'20'}\" showFilter=\"0\" showLayout=\"0\" itemWidth=\"47%\"]<br>[su_spacer]";
}
else if ($graph["name"]) {
($graph["name"] == "") ? $fb = "" : $fb = " of ".$graph["name"];
$return = "<a href=\"" . url_trim($matches[0],"http://") . "\">Facebook Profile". $fb ."</a><br>[su_spacer]";
}
else {
$return = "<a href=\"" . url_trim($matches[0],"http://") . "\">Facebook Profile</a><br>[su_spacer]";
}
return $return;
// ------------------------------------------------------------------------
case 'soundcloud.com':
$alter_error_handler = set_error_handler("mush_ErrorHandler");
$seite = file_get_contents("http://api.soundcloud.com/resolve.json?url=".url_trim($matches[0],"http://")."&client_id=c16efb34d50753daa59a5b6e9ef7ab44", false, $context);
restore_error_handler();
$json = json_decode($seite, true);
return "<br>[iframe src=\"https://w.soundcloud.com/player/?url=https%3A//".url_trim($json['uri'],"")."&auto_play=false&hide_related=false&visual=true\" width=\"100%\" height=\"450\"]<br>[su_spacer]";
// ------------------------------------------------------------------------
case 'example.com':
return "" . $matches[0] . "";
// ------------------------------------------------------------------------
default:
return "<a href=\"" . url_trim($matches[0],"http://") . "\">".url_trim($matches[0],"")."</a>";
}
},
$out);
$out = str_replace("<br />","<br>",$out);
$out = str_replace("/>","",$out);
// --------------------
// GOOGLE MAPS
// --------------------
if ( intval($lat) > 0 AND intval($lon) > 0 ) {
$out = str_replace("{{LOCATIONMAP}}", "
<tr>
<td class='party_head'> Map </td>
<td class='party_info'> [su_gmap width='540' height='500' responsive='yes' address='". $lat . "," . $lon . "']</td>
</tr>
", $out);
} else {
$out = str_replace("{{LOCATIONMAP}}", "", $out);
}
// ------------------------------------------------------------------------
// <<< END CREATE CONTENT
// ------------------------------------------------------------------------
} else {
$wp_post = NULL;
echo "<p> No example ID found !!! </p>";
}
if ( $wp_post ) {
// Update Party entry
$my_post = array(
'ID' => $post_id,
'post_title' => $nameParty,
'post_status' => 'publish',
'post_content' => $out,
'post_author' => 2,
'post_category' => array(118)
);
// ------------------------------------------------------------------------
// Update the post into the database
echo memory_get_usage()."<br>";
print_r($my_post);
// ------------------------------------------------------------------------
wp_update_post( $my_post );
// ------------------------------------------------------------------------
echo memory_get_usage()."<br>";
$base_url = get_site_url();
// ------------------------------------------------------------------------
echo "<p> UPDATE PARTY ENTRY (<a href='$base_url?p=$post_id'>$post_id</a>) $sort_records <br>
<a href='http://www.example.com/$event_id' target='_blank'><b>$nameParty</b></a><br>
$sub_head </p>";
} else {
// Create post object
$my_post = array(
'post_name' => $event_id,
'post_title' => $nameParty,
'post_content' => $out,
'post_author' => 2,
'post_date' => $dateCreated,
'post_date_gmt' => $dateCreated,
'post_type' => 'post',
'post_status' => 'publish',
'post_category' => array(118)
);
// Insert the post into the database
$post_id = wp_insert_post($my_post);
// $post_id = mush_get_post_id_by_name($event_id);
echo "<p> CREATE PARTY ENTRY (<a href='$base_url?p=$post_id'>$post_id</a>) $sort_records <br>
<a href='http://www.example.com/$event_id' target='_blank'><b>$nameParty</b></a><br>
$sub_head </p>";
}
if ( $meta_event_id == $event_id OR $meta_event_id == "" OR $meta_event_id == NULL) {
delete_post_meta( $post_id, "event_id");
add_post_meta( $post_id, "event_id", $event_id );
delete_post_meta( $post_id, "sub_head");
add_post_meta( $post_id, "sub_head", $sub_head );
delete_post_meta( $post_id, "sort_records");
add_post_meta( $post_id, "sort_records", $sort_records );
if ( get_post_meta( $post_id, "geo_location", TRUE )) {
delete_post_meta( $post_id, "geo_location");
add_post_meta( $post_id, "geo_location", $geo_location );
}
} else {
return "example ID <> example META INFO. PLEASE CHECK ID ".$event_id." / META-ID ".$meta_event_id;
}
}
}
?>
答案 0 :(得分:0)
我不确定确切原因,但是一旦处理了加载的文件,您可以尝试取消设置$xml
变量,同样尝试在不再使用时删除大变量(file_get_contents返回)。这可能会降低每个for
循环的内存需求。