EllisLab text mark
Advanced Search
     
Channel Entries API - setting member id
Posted: 28 June 2012 12:24 AM   [ Ignore ]
Joined: 2011-09-14
4 posts

I was working with the Channel Entries API to set up a system for a mobile user to post entries via an iOS application.

Is it possible to set the user via his id? 

$authorized = $this->EE->auth->authenticate_username($username, $password);

...

$data = array(
    ‘title’        =>  ‘test’,
    ‘entry_date’      => ‘1256953732’,
    ‘field_id_13’      => ‘testing content’,
    ‘field_id_57’      => ‘testing a custom field’,
    ‘field_id_58’      => ‘testing another custom field’,
              ‘member_id’ => 123
);

Testing with returns “You are not authorized to post in this channel”

 
Posted: 28 June 2012 12:47 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-09-14
4 posts

Bump…nobody is using the Channel Entries API than can some guidance?

 
Posted: 03 July 2012 07:41 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-15
426 posts

Here is some code to create a session for the user:

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


Here is some more code. This is useful because if you don’t do this checking before you use the Channel Entries API and you do have a permissions issue the API will through up its own HTML page which you might not want to happen.

$assigned_channels $this->EE->functions->fetch_assigned_channels();

$group_id $this->EE->session->userdata('group_id');

if (
$group_id !== '1')
{
 
if (!in_array($channel_id$assigned_channels))
 
{
      
//can't post
 
}
 
else
 
{
      
//can post
 
}

Also to set the ownership of the entry explicitly use author_id:

$data = array(
    
‘title’           =>  ‘test’,
    
‘entry_date’      => ‘1256953732’,
    
‘field_id_13’     => ‘testing content’,
    
‘field_id_57’     => ‘testing a custom field’,
    
‘field_id_58’     => ‘testing another custom field’,
    
‘author_id’       => 123
); 
 Signature 

ExpressionEngine Development | 21purple Web Studios, LLC or on Twitter @th3mus1cman

 
Posted: 03 July 2012 07:44 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-15
426 posts

Oh yeah, one more thing about the Channel Entries API. When you are creating a new entry be sure to pass entry_id = 0. Ex:

$data = array(
    
‘title’                =>  ‘test’,
    
'entry_id'          => 0,
    
‘entry_date’      => ‘1256953732’,
    
‘field_id_13’     => ‘testing content’,
    
‘field_id_57’     => ‘testing a custom field’,
    
‘field_id_58’     => ‘testing another custom field’,
    
‘author_id’       => 123
); 

If you don’t some 3rd party add-ons, mainly fieldtypes, but some extensions will blow up on you.

 Signature 

ExpressionEngine Development | 21purple Web Studios, LLC or on Twitter @th3mus1cman

 
Posted: 08 July 2012 09:18 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2011-09-14
4 posts

Got it.  Create a session, check for group, post my data with the author_id=>member_id and a zero for entry_id (Why the zero for the entry_id?). 

Thank you very much - that helps quite a bit.

 
Posted: 08 July 2012 09:22 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-15
426 posts

The CP has a hidden field for the entry_id set to 0 on new entries and some add-ons expect there to be a $data[‘entry_id’] key to be there because of that.

 Signature 

ExpressionEngine Development | 21purple Web Studios, LLC or on Twitter @th3mus1cman