PHP-Wordpress无法从URL所在的URL获取图像;我没有分机

PHP-Wordpress无法从URL所在的URL获取图像;我没有分机,php,wordpress,wordpress-rest-api,Php,Wordpress,Wordpress Rest Api,您好,我在wordpress获取特色图片时遇到问题,我通常使用此功能设置wordpress的图片 function generate_featured_images($post_id, $image, $title){ // only need these if performing outside of admin environment require_once(ABSPATH . 'wp-admin/includes/media.php'); require_onc

您好,我在wordpress获取特色图片时遇到问题,我通常使用此功能设置wordpress的图片

function generate_featured_images($post_id, $image, $title){
    // only need these if performing outside of admin environment
    require_once(ABSPATH . 'wp-admin/includes/media.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/image.php');

    // magic sideload image returns an HTML image, not an ID
    $media = media_sideload_image($image, $post_id);
    //$filename = basename($image);
    $info = pathinfo($image);
    $filename = sanitize_title($title).'.'.$info['extension'];

    // therefore we must find it so we can set it as featured ID
    if(!empty($media) && !is_wp_error($media)){
        $args = array(
            'post_type' => 'attachment',
            'posts_per_page' => -1,
            'post_status' => 'any',
            'post_parent' => $post_id
        );

        // reference new image to set as featured
        $attachments = get_posts($args);

        if(isset($attachments) && is_array($attachments)){
            foreach($attachments as $attachment){
                // grab source of full size images (so no 300x150 nonsense in path)
                $image = wp_get_attachment_image_src($attachment->ID, 'full');
                // determine if in the $media image we created, the string of the URL exists
                if(strpos($media, $image[0]) !== false){
                    // if so, we found our image. set it as thumbnail
                    set_post_thumbnail($post_id, $attachment->ID);
                    //resize_image_high($post_id);
                    // only want one image
                    break;
                }
            }
        }
    }
    return '';
}
为了调用该函数,我使用:

generate_featured_images( 123, "http://website.com/img/img1.jpg", "img name");
使用此函数可以正常工作

但是,问题来自图像url不显示扩展名,例如“”
我尝试不起作用,实际上我尝试搜索了几天,但没有找到解决方案,请帮助我。

同样的问题,但我认为它将适用于您的代码:

或者您可以添加一个额外的url检查,这里是我在插件上测试的代码:

$info = pathinfo($image);
$filename = sanitize_title($title).'.'.$info['extension'];
$image_url = $image;
if ( strpos( $image_url, 'shopee' ) !== false ) {
    $wp_filetype = wp_check_filetype( $filename, null );
    if ( !$wp_filetype['ext'] ) {
        $finfo = new finfo( FILEINFO_MIME_TYPE );
        $ext = $finfo->buffer($image_data);
        if ( $ext === 'image/jpeg' ) {
            $filename = $filename . '.jpg';
        }
    }   
}