3

So I'm building a site just now that isn't EE, however, I thought this would be a good time to learn how to get data into EE using the Channel Entries API when I'm under no pressure to get the job done.

In this scenario, I'm looking to sync data from the SoundCloud API into Channel Entries.

Can anyone point me in the direction of resources to pour over that could help give me a starter for 10?

I'm looking to sync playlists from SoundCloud that contains:

  • Title
  • ID
  • Secret URL
  • Enabled (boolean)
  • Private (boolean)
  • Tracks (Grid/Matrix)
    • Track ID
    • Track title
    • Track duration
    • Secret token
    • Stream URL
    • Image URL
    • Private track (boolean)
    • More info URL
    • iTunes URL
    • Amazon URL

Any guidance gratefully received.

2
  • Would be nice if the EE API documentation contained something on handling grids wouldn't it?
    – AllInOne
    Commented Oct 31, 2013 at 1:48
  • Not sure how helpful this is, but have you looked at putyourlightson.net/open-api Commented Oct 31, 2013 at 9:27

2 Answers 2

6

I'm not sure there are any resources specific to this use case, but I've built quite a few add-ons which import channel entries.

It's fairly straightforward with just a couple caveats:

  • Inserting data for more advanced fieldtypes can be a bit tricky. Your best bet is to look at what fields are actually being submitted via the publish form in the control panel and replicate those data structures programatically. You can easily find some examples for specific add-ons like Playa and Matrix with a little Googling.

  • You always want to make sure your data array is complete, especially if you're editing an existing entry. If fields are left out of the array when editing, they will get blanked out in the database.

Sample Code:

$this->EE->load->library('api');
$this->EE->api->instantiate('channel_entries');
$this->EE->api->instantiate('channel_fields');

$data = array(
    'title' => $entry->title,
    'field_id_4' => '',
    'field_ft_4' => 'none',
    'field_id_5' => $field_id_5_data,
    'field_ft_5' => 'none',
    'field_id_6' => $field_id_6_data,
    'field_ft_6' => 'none',
    'field_id_7' => $field_id_7_data,
    'field_ft_7' => 'none',
    'entry_date' => time(),
    'edit_date' => time()
  );

// This is how we'd set a Playa field
$data['field_id_4']['selections'] = array(1, 5, 7, 9);

$this->EE->api_channel_fields->setup_entry_settings(2, $data);
$this->EE->api_channel_entries->submit_new_entry(2, $data);

Note, this code was written before some recent API changes, so you will need to adjust the methods a bit to replace the deprecated ones with current ones.

Hopefully this helps you get going.

EDIT It should be noted, as discussed in the comments that a session is required for the channel entries API to work. grumble You'll need to fake it like this if you're going to do this from a CRON job or some other way that wouldn't already have a logged in member.

$this->EE->session->create_new_session($member_id);
$this->EE->session->fetch_session_data();
$this->EE->session->fetch_member_data();
4
  • Question: the EE API does require for there to be a valid logged in session set, Doesn't it? How would you do this from a cron type of call?
    – GDmac
    Commented Oct 31, 2013 at 10:51
  • 1
    Good point. If you need to do this from cron you will need to create a session to do it. I've edited the answer above with some code for doing that. Commented Oct 31, 2013 at 12:40
  • Thanks Jeremy, I guess I need to know how I'd go about actually consuming the source itself too. I guess that's largely dependent on the format they are giving though? Say for example the source was JSON or XML - any pointers there? Commented Oct 31, 2013 at 13:49
  • Sure. That might be best made a separate question. But the gist of it is that you'd use CURL to consume resources. Here's some basic PHP to help you get going: gist.github.com/anonymous/7250660 Commented Oct 31, 2013 at 14:24
1

I was originally going to post this as a comment, but it's too long, so have opted for an 'answer'.

You should be aware of the security implications of doing this as advised above.

I've just created an add-on to do just this which will be executed via CRON - however, it needs to be pointed out that running:

$this->EE->session->create_new_session($member_id);
$this->EE->session->fetch_session_data();
$this->EE->session->fetch_member_data();

instantiates itself as a user and creates a valid session, therefore if a user were to find and HTTP to the URL you've created for the CRON, it would effectively log them in to the site without any authentication credentials which would persist throughout the site posing a serious security risk.

Use with caution.

1
  • After editing the data, you need to delete the session again right ? Cron job or if you did something like a rest API How would that code look like ?
    – bomanden
    Commented May 20, 2016 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.