We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Importing data & images using custom controller - is this logic ok?

Development and Programming

tracerstar's avatar
tracerstar
1 posts
15 years ago
tracerstar's avatar tracerstar

Hi there,

I’m working on a site that uses this method of running custom CI controllers alongside the EE install http://is.gd/khRAs

It’s necessary to do this as we have a CI application that’s powered from an externally hosted DB (I’ll spare the details, but this is not something that can be worked around, and it’s working fine, so I’m happy there).

Part of the site is using an EE channel to handle news. The only thing, the news on the old site was handled by an RSS feed from an external data provider. This news needs to remain, but be integrated with the EE channel news.

The solution I’ve come up with is to import the news from the RSS into the entries table using the Channel Entries API. This works fine, and I’m happy with it (I can cronjob the controller to update regularly through the day).

Now, the problem I have comes down to images. The images are supplied as a URL on an external server. There’s a custom field in the news channel to handle file uploads. I want to grab the image and upload it using EE’s filemanager library so that image is then available to other news stories / the site (and it means I don’t have to have 2 custom fields for images and do checking on if it’s an external image or not in the template).

So, my question ultimately is does this code look like it’s ok? I don’t want to implement this and then risk potential future updates to the core system files breaking it. I tested it and it seems to work fine, but I’m worried it might be too much of a hack. Is this the best way to do image import along with an entry import, or is there another (cleaner?) solution?

Thanks, Ben.

//$data is an array of news items I grabbed from an RSS feed

//we can instantiate the EE API
$this->EE->load->library('api');
$this->EE->api->instantiate('channel_entries');

//load the file manager library so we can upload files
$this->EE->load->library('filemanager');
$this->filemanager->_initialize(array());
$dir = $this->filemanager->directory(1, FALSE, TRUE);

$channel_id = 1;//my news channel ID

//loop through the news items
foreach ($data as $entry) {

    //use CURL to grab the image from the other server and upload it to our news images directory
    $ch = curl_init($entry['img_url']);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata = curl_exec($ch);
    
    if(strpos($rawdata, 'Not Found') === false) {
        $img_path = rtrim($dir['server_path'], '/').'/'.$entry['image_name'];
        
        //write the image locally
        $fp = fopen($img_path, 'x');
        fwrite($fp, $rawdata);
        fclose($fp);
        
        //create_thumb using EE filemanager lib
        $this->filemanager->create_thumb($dir, array('name' => $entry['image_name']));
        
        //this is the value that is goes in the entry for an uploaded image
        $img_field = 'https://ellislab.com/asset/images/team-photo//'.$entry['image_name'];
    } else {
        //could save an image locally, so we don't assign an image to this article
        $img_field = '';
    }
    curl_close ($ch);
    
    
    $entry_data = array(
        'title' => $entry['headline'],
        'entry_date' => strtotime($entry['dateTime']),
        'field_id_1' => $entry['content'],
        'field_ft_1' => 'xhtml',
        'field_id_2' => $img_field,
        'field_ft_2' => 'xhtml',
    );
    //add the entry in EE
    $this->EE->api_channel_entries->submit_new_entry($channel_id, $entry_data);
}//end foreach loop
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.