• I’m building a plugin that will import some data from a third-party API.

    I have an array like

    $listings = json_decode('  "@odata.count": 986,
    
        "value": [
            {
                "@odata.id": "Property(6108754)",
                "City": "Reedsport",
                "ListPrice": 99950.0,
                "ListingKey": "6108754",
                "PropertySubType": "SingleFamilyResidence",
                "YearBuilt": null,
                "Media": [
                    {
                        "MediaURL": "image.com/image.jpg"
                     },
    
                    {
                        "MediaURL": "image.com/image.jpg"
                     }
    ]
    ]
    }')

    I have a foreach loop like

    foreach($listings->value as $listing) {

    foreach($listing->Media as $image){

    media_sideload_image($image->MediaURL);

    }

    /* the rest of the code to bring in the other field values */

    }

    The problem is the nested foreach loop to bring in the image breaks the site. If I remove the foreach loop for the images, the rest of the code works as expected.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It looks like $image->MediaURL values do not include the URL protocol such as “https://”. If so the URLs are incomplete and would cause sideload attempts to fail.

    If you continue having trouble after making that adjustment, check you error log to learn exactly how this code is breaking the site.

    Thread Starter oguruma

    (@oguruma)

    @bcworkz In actuality, it looks like “MediaURL”:”https://image.com/jpeg”

    I don’t know what changed, but it’s no longer breaking the site.

    Still, the code doesn’t run when I include the second foreach loop.

    foreach($listings->value as $listing) {
    
    
                /* Doesn't run if I include this 
    	foreach($listing->Media as $image){
        	media_sideload_image($image->MediaURL);
        }
    */
    
    
                $type_object = Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager->get_content_types( 'properties' );
    $handler = $type_object->get_item_handler();
    $item_id = $handler->update_item( array( 'listprice' => $listing->ListPrice,'listingkey' => $listing->ListingKey ) );
    }

    • This reply was modified 3 years, 1 month ago by oguruma.

    Hopefully this will help or point you in the right direction.

    In this version, we first loop through each listing in the $listings->value array. For each listing, we loop through the Media array and extract the MediaURL value into a new array called $media_urls. Once we have all the image URLs for a particular listing, we call media_sideload_image with the $media_urls array and the $listing->ID value. This should upload all the images for that listing in a single call to media_sideload_image.

    Note that I’ve assumed that each listing has a unique ID value that can be used to identify it in the call to media_sideload_image. You may need to adjust this depending on how your data is structured.

    foreach ($listings->value as $listing) {
        $media_urls = array();
        foreach ($listing->Media as $image) {
            $media_urls[] = $image->MediaURL;
        }
        media_sideload_image($media_urls, $listing->ID);
    }
    
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Best way to upload images by url on the backend?’ is closed to new replies.