2017-07-15 71 views
0

我有一个脚本,它使用simplexml_load_string来解析一个658kB的XML文件。该文件是一个属性(房地产)饲料与118个不同的属性总计21000行。该脚本使用了大量以下调用从节点提取数据:SImplexml缓慢还是WordPress的update_meta?

(string)$properties->address->county 

我还使用先进的自定义字段中的脚本来更新WordPress中的元数据的自定义字段,大量的多个呼叫:

update_field('field_59606d60525d3', (string)$properties->floorplans, $post_id); 

在Vagrant VVV盒上,脚本需要5分钟才能运行,然后超时。它设法加载到118个属性中约46个的自定义帖子类型。我不知道的是瓶颈。是它:

  • simplexml解析文件?
  • 在ACF中使用update_field?

Webgrind(xdebug)似乎指向很多update_meta调用,但我不确定在cachegrind文件中查找和理解什么。

我想什么,我问的是有没有更快的替代方案,以PHP本地的SimpleXML以及如何做一个解释了XDebug/webgrind输出

该脚本将最终商品运行的托管(无VPS /专用)

技能等级:程序(功能,NOT班)

Xdebug的输出:

Call Stack 
# Time Memory Function Location 
1 0.2021 361704 {main}() .../test.php:0 
2 0.6501 5888288 get_xml() .../test.php:163 
3 544.3322 115472480 update_field(string(19), array(457), long) .../test.php:115 
4 544.3325 115472480 acf_update_value(array(457), long, array(24)) .../api-template.php:1018 
5 544.3325 115472536 apply_filters(string(30), array(457), long, array(24)) .../api-value.php:350 
6 544.3325 115472936 WP_Hook->apply_filters(array(457), array(3)) .../plugin.php:203 
7 544.3326 115473688 acf_field_repeater->update_value(array(457), long, array(24)) .../class-wp-hook.php:298 
8 556.4756 117433368 acf_field_repeater->update_row(array(2), long, array(24), long) .../repeater.php:900 
9 556.4756 117434744 acf_update_value(string(42), long, array(20)) .../repeater.php:804 
10 556.5003 117437600 acf_update_metadata(long, string(15), string(19), true) .../api-value.php:368 
11 556.5004 117438016 update_metadata(string(4), long, string(15), string(19), ???) .../api-value.php:101 
12 556.5005 117438136 get_metadata(string(4), long, string(15), ???) .../meta.php:193 
13 556.5005 117438512 update_meta_cache(string(4), array(1)) .../meta.php:497 
14 556.5124 118050992 intval (string(3)) .../meta.php:830 

更新#1 2017年1月8日

我在这个阶段,我决定file_get_contents可能是问题,因为在饲料中的每个属性都有相关的大约10至15的图像的URL。 118个属性=仅有的1800个图像需要URL调用。我试过cUrl,然后偶然发现了curl_multi

我现在已经得到了下面的工作代码,将curl_multi上的图像URL数组,将它们添加到WP作为附件,并将它们附加到特定的post_id,同时更新ACF图库字段。不过,我对这个实际速度是否更快并不明智?我该如何处理这样的问题,或者如果curl_multi实际上是在异步执行操作,或者我的代码是正确的,那么我该如何处理?

require_once('/srv/www/broadbean/wp-blog-header.php'); 
require_once('/srv/www/broadbean/wp-admin/includes/media.php'); 
require_once('/srv/www/broadbean/wp-admin/includes/file.php'); 
require_once('/srv/www/broadbean/wp-admin/includes/image.php'); 

// https://stackoverflow.com/questions/15436388/download-multiple-images-from-remote-server-with-php-a-lot-of-images 
// http://php.net/manual/en/function.curl-multi-init.php 

$post_id = '2773'; 
$image_urls = array('http://target.domain.net/photos/1334268.jpg', 'http://target.domain.net/photos/1278564.jpg', 'http://target.domain.net/photos/1278565.jpg'); 
$chs = array(); 
$upload_dir = wp_upload_dir(); 
$tc = count($image_urls); 
$cmh = curl_multi_init(); 

for ($t = 0; $t < $tc; $t++) 
{ 
    $chs[$t] = curl_init(); 
    curl_setopt($chs[$t], CURLOPT_URL, $image_urls[$t]); 
    //curl_setopt($chs[$t], CURLOPT_FILE, $fp); 
    curl_setopt($chs[$t], CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($chs[$t], CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($chs[$t], CURLOPT_TIMEOUT, 120); 
    curl_setopt($chs[$t], CURLOPT_USERAGENT, 'Mozilla/5.0'); 
    curl_multi_add_handle($cmh, $chs[$t]); 
} 

$running = null; 

do { 
    curl_multi_exec($cmh, $running); 
} while ($running); 


for ($t = 0; $t < $tc; $t++) 
{ 
    $filename = basename($image_urls[$t]); 
    $image_file = $upload_dir['path'] . '/' . $filename; 
    $fp = fopen($image_file, 'w+'); 

    fwrite($fp, curl_multi_getcontent($chs[$t])); 
    fclose($fp); 

    $wp_filetype = wp_check_filetype($image_file, null); 

    $attachment = array(
     'post_mime_type' => $wp_filetype['type'], 
     'post_title' => sanitize_file_name($filename), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 


    $attach_id = wp_insert_attachment($attachment, $image_file, $post_id); 
    $attach_data = wp_generate_attachment_metadata($attach_id, $image_file); 
    $update_attach_metadata = wp_update_attachment_metadata($attach_id, $attach_data); 

    $add_gallery_images[] = $attach_id; 

    curl_multi_remove_handle($cmh, $chs[$t]); 
    curl_close($chs[$t]); 

} 
var_dump($add_gallery_images); 
update_field('field_5973027c18fdc', $add_gallery_images , $post_id); 

curl_multi_close($cmh); 

回答

0

对于您的问题并非真正具体的答案,但由于您使用的是Wordpress,您是否尝试过使用WP All Import?它对ACF也有很好的实现(我认为你需要在这种情况下的专业版)

+0

我没有,但这是一个很好的观点。我有一个使用它将MS Access导入导入ACF的同事,效果很好(约4000条记录)。 WP All Import和'手动'之间有什么不同?也许我需要发布代码片段? – tringwebdesign

+0

那么主要的好处是,WP的所有导入插件非常适合做这种特别的操作。 如果你真的想这样做,而不使用现有的插件/库或这样,那么你应该知道所有的做的和不的WordPress的(在这种情况下高级自定义字段),这可能会导致编写定制的MySQL查询。 – robinl