2017-07-15 68 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