首页 > 解决方案 > 无法使用 PHP 代码从轮播/滑块/幻灯片中抓取所有图像使用 PHP 简单 HTML DOM 解析器

问题描述

我正在开发一个插件,该插件将从用户那里获取产品 URL,然后单击按钮会抓取产品数据,例如:标题:

$title =  $html->find('title', 0)->plaintext;

描述:

if(!empty($html->find('div.woocommerce-Tabs-panel--description', 0)->plaintext)){
            $description = $html->find('div.woocommerce-Tabs-panel--description', 0)->plaintext;
        }else if(!empty($html->find('div.product-description', 0)->plaintext)){
            
                $description = $html->find('div.product-description', 0)->plaintext;
            
        }

价格:

if(!empty($html->find('p.price', 0)->plaintext)){
            $price = $html->find('p.price', 0)->plaintext;
        }else if(!empty($html->find('span.price', 0)->plaintext)){
            
                $price = $html->find('span.price', 0)->plaintext;
            
        }if(!empty($html->find('p.price', 0)->plaintext)){
            $price = $html->find('p.price', 0)->plaintext;
        }else if(!empty($html->find('span.price', 0)->plaintext)){
            
                $price = $html->find('span.price', 0)->plaintext;
            
        }

和图片:

$image_a_tag = $html->find('a.yith_magnifier_thumbnail');
$image_href_array = array();
$image_id_array = uploadAndAttachImageId($image_href_array);
so_upload_all_images_to_product($post_id, $image_id_array);
    function uploadAndAttachImageId($image_href_array){
        $image_id_array = array();
        foreach ($image_href_array as $image_url) {

            //$image_url = $image_url->href;
            $upload_dir = wp_upload_dir();

            $image_data = file_get_contents( $image_url );

            $filename = basename( $image_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' => sanitize_file_name( $filename ),
            'post_content' => '',
            'post_status' => 'inherit'
            );

            $attach_id = wp_insert_attachment( $attachment, $file );
            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 );



            array_push($image_id_array, $attach_id);
        }
        return $image_id_array;
    }

    // function to add multiple images

    function so_upload_all_images_to_product($product_id, $image_id_array) {
        //take the first image in the array and set that as the featured image
        //set_post_thumbnail($product_id, $image_id_array[0]);

        update_post_meta( $product_id, '_thumbnail_id' , $image_id_array[0]);

    
        //if there is more than 1 image - add the rest to product gallery
        if(sizeof($image_id_array) > 1) { 
            array_shift($image_id_array); //removes first item of the array (because it's been set as the featured image already)
            update_post_meta($product_id, '_product_image_gallery', implode(',',$image_id_array)); //set the images id's left over after the array shift as the gallery images
        }
    }

使用:PHP 简单 Html 解析器 到目前为止,我能够执行所需的操作,也能够将数据上传到 woo 商务产品,但无法从轮播/滑块中检索图像。例如https://www.ebay.co.uk/itm/274305342678 我已经搜索了很多,但没有我能找到的编程方式来做这样的事情。提前致谢!

标签: phpwordpresswoocommercesimple-html-dom

解决方案


推荐阅读