Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SImplexml速度慢还是WordPress update\u meta?_Php_Wordpress_Xdebug_Advanced Custom Fields_Curl Multi - Fatal编程技术网

Php SImplexml速度慢还是WordPress update\u meta?

Php SImplexml速度慢还是WordPress update\u meta?,php,wordpress,xdebug,advanced-custom-fields,curl-multi,Php,Wordpress,Xdebug,Advanced Custom Fields,Curl Multi,我有一个脚本,它使用simplexml_load_字符串解析658kB的XML文件。该文件是一个property(real estate)提要,包含118个不同的属性,总计21000行。该脚本使用以下许多调用从节点提取数据: (string)$properties->address->county 我还在脚本中使用高级自定义字段更新WordPress中的元数据自定义字段,还有很多调用: update_field( 'field_59606d60525d3', (string)$pr

我有一个脚本,它使用simplexml_load_字符串解析658kB的XML文件。该文件是一个property(real estate)提要,包含118个不同的属性,总计21000行。该脚本使用以下许多调用从节点提取数据:

(string)$properties->address->county
我还在脚本中使用高级自定义字段更新WordPress中的元数据自定义字段,还有很多调用:

update_field( 'field_59606d60525d3', (string)$properties->floorplans, $post_id );
在一个流浪的VVV箱上,脚本运行需要5分钟以上,然后超时。它成功地将118个属性中的46个加载到自定义帖子类型中,但我不知道瓶颈是什么。是:

  • simplexml解析文件
  • 是否在ACF中使用更新_字段
Webgrind(xdebug)似乎指向很多update\u meta调用,但我不确定在cachegrind文件中查找和理解什么

我想我要问的是,有没有比PHP原生simpleXML更快的替代方案,以及如何解释XDEBUG/webgrind输出

此脚本最终将在商品托管上运行(无VPS/专用)

技能水平:程序(功能,而非类别)

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年8月1日

我正处于这样一个阶段,我决定
file\u get\u contents
可能是问题所在,因为提要中的每个属性都有大约10到15个相关的图像URL。118 properties=只差1800个图像URL调用。我试了一下cUrl,然后偶然发现了
cUrl\u multi

我现在有了下面的工作代码,它将
curl\u multi
放在一组图像URL上,将它们作为附件添加到WP中,并在更新ACF gallery字段时将它们附加到特定的
post\u id
。然而,我仍然不知道这是否真的更快?我如何计算这样的时间,或者计算出
curl\u 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);
require_once('/srv/www/broadbean/wp blog header.php');
需要一次('/srv/www/broadbean/wp admin/includes/media.php');
require_once('/srv/www/broadbean/wp admin/includes/file.php');
需要一次('/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\u url=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=计数($image\u url);
$cmh=curl_multi_init();
对于($t=0;$t<$tc;$t++)
{
$chs[$t]=curl_init();
curl_setopt($chs[$t],CURLOPT_URL,$image_URL[$t]);
//curl_setopt($chs[$t],CURLOPT_文件,$fp);
curl_setopt($chs[$t],CURLOPT_FOLLOWLOCATION,1);
curl_setopt($chs[$t],CURLOPT_RETURNTRANSFER,1);
curl_setopt($chs[$t],CURLOPT_超时,120);
curl_setopt($chs[$t],CURLOPT_USERAGENT,'Mozilla/5.0');
卷曲多加手柄($cmh,$chs[$t]);
}
$running=null;
做{
curl_multi_exec($cmh,$running);
}同时($运行);
对于($t=0;$t<$tc;$t++)
{
$filename=basename($image_url[$t]);
$image_file=$upload_dir['path']./'.$filename;
$fp=fopen($image_文件'w+');
fwrite($fp,curl_multi_getcontent($chs[$t]));
fclose($fp);
$wp\u filetype=wp\u check\u filetype($image\u file,null);
$attachment=array(
'post_mime_type'=>$wp_filetype['type'],
“post_title”=>清理文件名($filename),
'发布内容'=>'',
“post_状态”=>“继承”
);
$attach\u id=wp\u insert\u attachment($attachment,$image\u file,$post\u id);
$attach\u data=wp\u生成\u附件\u元数据($attach\u id,$image\u file);
$update\u attach\u metadata=wp\u update\u attachment\u metadata($attache\u id,$attache\u data);
$add_gallery_images[]=$attach_id;
卷曲多移除手柄($cmh,$chs[$t]);
旋度闭合($chs[$t]);
}
var_dump($add_gallery_images);
更新_字段('field_5973027c18fdc',$add_gallery_images,$post_id);
卷曲多合($cmh);

这并不是您问题的具体答案,但既然您使用的是Wordpress,您是否尝试过使用WP All Import?它也有一个很好的ACF实现(我想在这种情况下你需要专业版)

我没有,但这是一个很好的观点。我有一位同事使用它将MS Access导出导入ACF,效果良好(约4000条记录)。但是,WP全部导入和“手工”导入之间有什么不同?也许我需要发布一段代码片段?主要的好处是WP-All-Import插件针对这类操作进行了很好的优化。如果你真的想在不使用现有插件/库的情况下完成这项工作,那么你应该知道Wordpress的所有“做”和“不做”的地方(在本例中是“高级自定义字段”),这可能会导致编写自定义mysql查询。