phpapiconcatenationwufoo

Concatenating Wufoo API results


I'm working with the Wufoo api (https://{subdomain}.wufoo.com/api/v3/forms/{formIdentifier}/entries.{xml|json}) trying to get all of my entries, currently around 150, as one php array. Wufoo limits the number of entries returned to 100. So now I have two php arrays that I would like to concatenate/combine into a single array.

The Code so far:

$api_uri_1 = "https://example.wufoo.com/api/v3/forms/example-form/entries.json?pageStart=0&pageSize=100";
$api_uri_2 = "https://example.wufoo.com/api/v3/forms/example-form/entries.json?pageStart=1&pageSize=100";

function wufoo_api($api_uri) {
  $curl = curl_init($api_uri);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_USERPWD, 'WUFOO-API-KEY-HERE:password');
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_USERAGENT, 'Wufoo Sample Code');
  $response = curl_exec($curl);
  $resultStatus = curl_getinfo($curl);

  if ($resultStatus['http_code'] == 200) {
      $results = json_decode($response, true);
      return $results;
  } else {
      $results = 'Call Failed '.print_r($resultStatus);
      return $results;
  }
}

$result1 = wufoo_api($api_uri_1);
$result2 = wufoo_api($api_uri_2);

What I've tried and hasn't worked

$all_results = array_merge($result1, $result2);

and something like this

$all_results = $result1;
$all_results += $result2;

How can I concatenate/combine result1 and result2

Thanks in advance for any help

Updated: What worked

Thanks to Ben I realized I needed to use an array key to target the parts of the array I needed.

$all_results = array_merge($result1['Entries'],$result2['Entries']); 

Solution

  • The result from json_decode will contain the root element (Entries according to the api).

    You would access the array of entries through $result1['Entries'], so to concatenate the entries, you'd need to do something like:

    $all_results = array_merge($result1['Entries'],$result2['Entries']);
    

    or

    $all_results = $result1['Entries'];
    $all_results += $result2['Entries'];
    

    also for the fail case, it should be

    $results = 'Call Failed '.print_r($resultStatus,true);
    

    (otherwise, you're outputting the result of print_r rather than returning it)