Combine results of 2 InfusionSoft queries into 1 collection

Situation: I’m writing a PHP app that includes InfusionSoft calls via the API. I have a string that may be part of a contact’s given name or family name. I want to either a) run one search to cover both possibilities and get back one collection that includes all possible matches; or b) run one search for Given name and one search for Family name, then combine the two results into one collection.

I tried running one search with an orWhere() in the conditions, but was told that InfusionSoft API doesn’t support an orWhere. I tried running separate searches and then combining them, but found that using a forEach loop on a collection of results from an InfusionSoft query doesn’t work. If I write

forEach($results as $result) {
dd($result);
}
the dd() command shows that $result is empty.

What am I doing wrong?

Good afternoon Sean,

I’m not really a PHP developer by trade (we work using Java for the most part), but if you provide a more complete code snippet of what you’re trying to accomplish, someone may be able to give some guidance. Also, just to verify, are you using GitHub - infusionsoft/infusionsoft-php: PHP client library for the Infusionsoft API. or are you making calls directly against the API using another toolkit?

Thanks!

  • Tom Scott
    Keap API Engineer

Hi TomScott. Sorry for the delay in replying - I didn’t see this til this morning. I’m using the Infusionsoft PHP SDK found at your Github link. egarding your request for more detailed code, this is what I’m using right now. It works, but I have a feeling there’s a better way to do it.

$query = $infusionsoft->contacts()
->where([‘family_name’ => $search])
->get();
$familyResults = $query->all();
foreach($familyResults as $result) {
$contact = [
‘id’ => $result->id,
‘first_name’ => $result->given_name,
‘last_name’ => $result->family_name,
‘email’ => $result->email_addresses[0][‘email’],
];
$contactsCollection = $contact;
}
$query = $infusionsoft->contacts()
->where([‘given_name’ => $search])
->get();
$givenResults = $query->all();
foreach($givenResults as $result) {
$contact = [
‘id’ => $result->id,
‘first_name’ => $result->given_name,
‘last_name’ => $result->family_name,
‘email’ => $result->email_addresses[0][‘email’],
];
$contactsCollection = $contact;
}

Also, is there a way that you know of to run a soft search or partial string search with the Infusionsoft API? For example, a search with the string “John” would match not only copntacts with the first name “John”, but also ones with first name of “Jon”, or last name of “Johnson”?