Support

Home Forums Event Espresso Premium Using API – Pulling People records by email

Using API – Pulling People records by email

Posted: March 26, 2021 at 11:21 am

Viewing 8 reply threads


make_webmaster

March 26, 2021 at 11:21 am

We have a process where we use the API to create events, tickets and assign People as owners to the event. I have the API working fine to create the event and tickets, but I am struggling with assigning People to the group. I need to first search if a People object exists based on email. If it does, then I assign that People. If it doesn’t then I create a new People object and assign that.

I cannot figure out how to search for a People object by email.
I have tried – http://myurl.com/wp-json/ee/v4.8.36/where%5BPeople.PER_email%5D=some_email@gmail.com’
as well as
$request = new WP_REST_Request(‘GET’, ‘/ee/v4.8.36/people’);
$request->set_body_params(array(‘where’ => ‘[People.PER_email]=johncollins@sonic.net’));

Both error out. Can someone help me find the correct syntax to pull a People object by email?


make_webmaster

March 29, 2021 at 10:26 am

Any help would be appreciated


make_webmaster

March 30, 2021 at 8:44 am

I could really use some help here!!!


Seth Shoultes

  • Support Staff

March 30, 2021 at 10:15 am

Hi there,

Sorry, we missed your request. I’ll ping one of our developers to see if they have some feedback on this.


Brent Christensen

  • Support Staff

March 30, 2021 at 11:15 am

Hi make_webmaster

I’m Event Espresso’s lead developer.
The format for adding where conditions for your use case would be like this:


/wp-json/ee/v4.8.36/people?where[PER_email]=address@domain.com

HOWEVER…

the Person custom post type requires authentication to access, so if you ONLY enter a query like the above, you should see something like:


{
  "code": "rest_people_cannot_list",
  "message": "Sorry, you are not allowed to list people. Missing permissions: ee_read_peoples,ee_read_others_peoples,ee_read_private_peoples",
  "data": {
    "status": 403
  }
}

so there are two very important aspects of this that are required to make things work:

1. the user wishing to pull Person data via the REST API needs to have one or more of the following capabilities:
– ee_read_peoples
– ee_read_others_peoples
– ee_read_private_peoples

2. the user has to be authenticated

for the latter point, you can find more information about that here:
https://github.com/eventespresso/event-espresso-core/blob/master/docs/C–REST-API/ee4-rest-api-introduction.md#authentication

so if you have everything setup correctly, the URL should look something like this:


http://www.dev.test/wp-json/ee/v4.8.36/people?_authorization=Basic+YWRtaW46YWRtaW4%3D&_wpnonce=cafcba578a&where[PER_email]=bilbo@theshire.com

that’s a URL from my local test server that i used to test that things work. It returned the following response:


[
  {
    "PER_ID": 34,
    "PER_full_name": "Bilbo Baggins",
    "PER_bio": {
      "raw": "",
      "rendered": ""
    },
    "PER_slug": "bilbo-baggins",
    "PER_created": "2021-03-30T02:30:09",
    "PER_short_bio": "",
    "PER_modified": "2021-03-30T02:30:09",
    "PER_wp_user": 1,
    "PER_parent": 0,
    "status": {
      "raw": "publish",
      "pretty": "Published"
    },
    "comment_status": "closed",
    "ping_status": "closed",
    "PER_fname": "Bilbo",
    "PER_lname": "Baggins",
    "PER_address": "123 hobbit hole lane",
    "PER_address2": "",
    "PER_city": "Shire",
    "STA_ID": 0,
    "CNT_ISO": "",
    "PER_zip": "",
    "PER_email": "bilbo@theshire.com",
    "PER_phone": "",
    "PER_created_gmt": "2021-03-30T09:30:09",
    "PER_modified_gmt": "2021-03-30T09:30:09",
    "link": "http:\/\/www.dev.test\/people\/bilbo-baggins\/",
    "_links": {
      "self": [
        {
          "href": "http:\/\/www.dev.test\/wp-json\/ee\/v4.8.36\/people\/34"
        }
      ],
      "collection": [
        {
          "href": "http:\/\/www.dev.test\/wp-json\/ee\/v4.8.36\/people"
        }
      ],
      "https:\/\/api.eventespresso.com\/events": [
        {
          "href": "http:\/\/www.dev.test\/wp-json\/ee\/v4.8.36\/people\/34\/events",
          "single": false
        }
      ],
      "https:\/\/api.eventespresso.com\/person_posts": [
        {
          "href": "http:\/\/www.dev.test\/wp-json\/ee\/v4.8.36\/people\/34\/person_posts",
          "single": false
        }
      ],
      "https:\/\/api.eventespresso.com\/state": [
        {
          "href": "http:\/\/www.dev.test\/wp-json\/ee\/v4.8.36\/people\/34\/state",
          "single": true
        }
      ],
      "https:\/\/api.eventespresso.com\/country": [
        {
          "href": "http:\/\/www.dev.test\/wp-json\/ee\/v4.8.36\/people\/34\/country",
          "single": true
        }
      ],
      "https:\/\/api.eventespresso.com\/term_taxonomies": [
        {
          "href": "http:\/\/www.dev.test\/wp-json\/ee\/v4.8.36\/people\/34\/term_taxonomies",
          "single": false
        }
      ],
      "https:\/\/api.eventespresso.com\/post_metas": [
        {
          "href": "http:\/\/www.dev.test\/wp-json\/ee\/v4.8.36\/people\/34\/post_metas",
          "single": false
        }
      ],
      "https:\/\/api.eventespresso.com\/extra_metas": [
        {
          "href": "http:\/\/www.dev.test\/wp-json\/ee\/v4.8.36\/people\/34\/extra_metas",
          "single": false
        }
      ],
      "https:\/\/api.eventespresso.com\/change_logs": [
        {
          "href": "http:\/\/www.dev.test\/wp-json\/ee\/v4.8.36\/people\/34\/change_logs",
          "single": false
        }
      ]
    },
    "_calculated_fields": {
      "_protected": []
    },
    "_protected": []
  }
]

Hope that helps

  • This reply was modified 3 years, 8 months ago by Brent Christensen. Reason: formatting


make_webmaster

March 30, 2021 at 11:55 am

Hi Brent,

Thank you so much for replying! We are using a submitted form to create events and I would prefer doing all of the API calls internally so I don’t have to deal with authentication.

I have WP_REST_Request working great with POST calls, but I cannot get it to work with the GET. If I do this call, it returns all People objects, not the one that matches the specific email:

$request = new WP_REST_Request(‘GET’, ‘/ee/v4.8.36/people’);
$request->set_query_params([‘where[PER_email]’=>’some_email@gmail.com’]);
$response = rest_do_request($request);

What am I doing wrong here?


Josh

  • Support Staff

March 30, 2021 at 7:22 pm

Hi,

The where conditions need to be in an array. e.g.

$request = new WP_REST_Request('GET', '/ee/v4.8.36/people');
$request->set_query_params(
  array(
    'where' => array('PER_email' => 'some_email@gmail.com')
  )
);
$response = rest_do_request($request);


Brent Christensen

  • Support Staff

March 30, 2021 at 7:35 pm

We are using a submitted form to create events and I would prefer doing all of the API calls internally so I don’t have to deal with authentication.

Oh… uhh… if the form is submitted to the same server that Event Espresso is installed on, then using the REST API to obtain the data is extremely inefficient since you are essentially sending a second request to get it.

But to answer your question, the request needs to be formatted like this:

$request = new WP_REST_Request('GET', '/ee/v4.8.36/people');
$request->set_query_params([
    'where' => [ 'PER_email' => 'bilbo@theshire.com' ]
]);

Now that said, you should seriously consider looking into using Event Espresso’s model system for retrieving the data you need because it will be easier and more efficient than making additional REST requests.

To get a single Person object based on their email address, you simply need to do something like:

$person = EEM_Person::instance()->get_one([['PER_email' => 'bilbo@theshire.com']]);

then you can get any of that person’s details using any of the methods found in the EE_Person class, like

echo $person->full_name();
echo $person->full_address_as_array();
echo $person->email();
echo $person->phone();

Plus our model system will automagically resolve relations for you, so let’s say you then wanted to get a list of events associated with that person, you could simply do something like:

$event = $person->get_many_related('Event');
echo $event->name();
echo $event->description();
echo $event->phone();
// gets an array of EE_Ticket objects
$tickets = $event->tickets();

If interested, you can find out more about our model system here:
https://github.com/eventespresso/event-espresso-core/tree/master/docs/G–Model-System


make_webmaster

March 31, 2021 at 9:17 am

Thank you both!!! Josh, that is exactly what I needed to make that statement work. However Brent, I didn’t even think of using the model system. I will switch over to that right away.

Thank you again!

Viewing 8 reply threads

The support post ‘Using API – Pulling People records by email’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso