How to side load an image from a service

I am currently working on a WordPress plugin that creates dummy content. I’d like to also have the plugin create random images using a service like http://lorempixel.com. The problem however is that when accessing the service that an actual file name isn’t generated. You generate a random image like this:

http://lorempixel.com/1200/700/

How can I use the media_handle_sideload() function to accomplish this?

Solutions Collecting From Web of "How to side load an image from a service"

Short answer, you don’t. Functions like sideload are for importing images into WordPress itself and using all the normal media library stuff. For images on an external service, use normal img tags.

If you want to copy an image from a URL, you sideload it. Not otherwise.

Okay… I’ve solved this…

@otto – looks like all I had to do was use a bit of curl magic to do this…

code below as part of an advanced wordpress dummy page / post / image / text creator

function render_page() {
    if ($_POST['delete']) {
        $num_posts_deleted = 0;
        $num_images_deleted = 0;
        global $wpdb;

        $SQL = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wpl_dummy'";
        $rows = $wpdb->get_results($SQL);

        foreach ($rows as $row) {
            $num_posts_deleted++;
            wp_delete_post( $row->post_id, true );
        }

        $SQL = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wpl_dummy_image'";
        $rows = $wpdb->get_results($SQL);

        foreach ($rows as $row) {
            $num_images_deleted++;
            wp_delete_attachment( $row->post_id, true );
        }

    }
    if ($_POST['create']) {
        $num_posts_created = 0;
        // Build the options array
        $options = array();

        if ($_POST['number_of_paragraphs'])
            $options[] = $_POST['number_of_paragraphs'];

        if ($_POST['paragraph_length'])
            $options[] = $_POST['paragraph_length'];

        if ($_POST['decorate'])
            $options[] = 'decorate';

        if ($_POST['links'])
            $options[] = 'links';

        if ($_POST['ul'])
            $options[] = 'ul';

        if ($_POST['ol'])
            $options[] = 'ol';

        if ($_POST['dl'])
            $options[] = 'dl';

        if ($_POST['bq'])
            $options[] = 'bq';

        if ($_POST['code'])
            $options[] = 'code';

        if ($_POST['headers'])
            $options[] = 'headers';

        if ($_POST['ac'])
            $options[] = 'allcaps';

        if ($_POST['pr'])
            $options[] = 'prude';

        if ($_POST['fi'])
            $featured_image = 1;

        if ($_POST['fi_width'])
            $featured_image_width = $_POST['fi_width'];

        if ($_POST['fi_height'])
            $featured_image_height = $_POST['fi_height'];

        $options = implode('/', $options);


        // Match up
        $args = array(
            'public'=>true
        );
        $post_types = get_post_types( $args, 'objects', 'and' );


        if ( $featured_image )
            require_once( ABSPATH . 'wp-admin/includes/image.php' );

        foreach ( $post_types as $post_type ) {
            if ( $post_type->name != 'attachment') {
                if ( $_POST[$post_type->name]) {
                    for ($i=1; $i <= $_POST['number_of_posts_to_create']; $i++) {
                        // Get the random text
                        $curl = curl_init();
                        curl_setopt_array($curl, array(
                            CURLOPT_RETURNTRANSFER => 1,
                            CURLOPT_URL => 'http://loripsum.net/api/' . $options
                        ));
                        $resp = curl_exec($curl);
                        curl_close($curl);

                        // Massage our data
                        preg_match("/<h1 ?.*>(.*)<\/h1>/", $resp, $matches);
                        $title = $matches[1];
                        $body = str_replace($matches[0], '', $resp);

                        // Create the post
                        $post = array(
                            'post_content'  => $body,
                            'post_title'    => $title,
                            'post_type'     => $post_type->name,
                            'post_status'   => 'publish'
                        );  
                        $post_id = wp_insert_post( $post, $wp_error );
                        update_post_meta($post_id, '_wpl_dummy', '1');


                        // Add and attach a featured image if necessary
                        if ( $featured_image ) {
                            $curl = curl_init();
                            curl_setopt_array($curl, array(
                                CURLOPT_RETURNTRANSFER => 1,
                                CURLOPT_URL => 'http://lorempixel.com/' . $featured_image_width . '/' . $featured_image_height . '/'
                            ));
                            $img_raw = curl_exec($curl);
                            curl_close($curl);

                            $upload_dir = wp_upload_dir();

                            // place our file on the server in the appropriate directory
                            $file = $upload_dir['path'] . '/wpl_' . time() . '_dummy.jpg';
                            file_put_contents ( $file , $img_raw );

                            // Check the type of tile. We'll use this as the 'post_mime_type'.
                            $filetype = wp_check_filetype( basename( $file ), null );;

                            // Prepare an array of post data for the attachment.
                            $attachment = array(
                                'guid'           => $upload_dir['url'] . '/' . basename( $file ), 
                                'post_mime_type' => $filetype['type'],
                                'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $file ) ),
                                'post_content'   => '',
                                'post_status'    => 'inherit'
                            );

                            // Insert the attachment.
                            $attach_id = wp_insert_attachment( $attachment, $file, $post_id );              

                            // Generate the metadata for the attachment, and update the database record.
                            $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
                            wp_update_attachment_metadata( $attach_id, $attach_data );


                            // Update the featured image
                            update_post_meta($post_id, '_thumbnail_id', $attach_id);
                            update_post_meta($attach_id, '_wpl_dummy_image', '1');
                        }

                        $num_posts_created++;
                    }
                }
            }
        }


    }

}
?>

obviously more to it but this handles what I was aiming to accomplish. Thanks for the nudge in the proper direction.