Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Maria30's avatar
Level 21

loop throught api results

Hi to all!

I get results from an API but they have a limit of 100 results for each request. How can i take more than that?

This is my code (from a video of @bobbybouwmann)

 class TrialsApiService
{


    public function fullStudies(): Collection
    {

        $client = new Client();

        $min_rank = 1;
        $max_rank = 100;



            $response = $client->get("https://clinicaltrials.gov/api/query/full_studies?min_rnk=$min_rank&max_rnk=$max_rank&fmt=json");

            $body = json_decode((string)$response->getBody(), true);


            $collection = collect($body['FullStudiesResponse']['FullStudies'])->map(function ($fullStudies) {

                return [
                    'Rank' => $fullStudies['Rank'],
                    'Status' => $fullStudies['Study']['ProtocolSection']['StatusModule']['OverallStatus'],
                    'Condition' => $fullStudies['Study']['ProtocolSection']['ConditionsModule']['ConditionList']['Condition'][0],
                    'NCTId' => $fullStudies['Study']['ProtocolSection']['IdentificationModule']['NCTId'],
                    'Organization' => $fullStudies['Study']['ProtocolSection']['IdentificationModule']['Organization']['OrgFullName'],
                    'Title' => $fullStudies['Study']['ProtocolSection']['IdentificationModule']['BriefTitle'],
                    'Sponsor' => $fullStudies['Study']['ProtocolSection']['SponsorCollaboratorsModule']['LeadSponsor']['LeadSponsorName']

                ];


            });

        return $collection;
    }

}

0 likes
12 replies
Ajvanho's avatar

Probably to increase $max_rank

 $max_rank = ? ;
guybrush_threepwood's avatar
public function fullStudies(): Collection
{
    $studies = collect();

    $client = new Client();

    $min_rank = 1;
    $max_rank = 100;
    $limit = 100;
    $max_records = 1000;

    do {

        $response = $client->get("https://clinicaltrials.gov/api/query/full_studies?min_rnk=$min_rank&max_rnk=$max_rank&fmt=json");

        $body = json_decode((string)$response->getBody(), true);

        $collection = collect($body['FullStudiesResponse']['FullStudies'])->map(function ($fullStudies) {
             return [
                 'Rank' => $fullStudies['Rank'],
                 'Status' => $fullStudies['Study']['ProtocolSection']['StatusModule']['OverallStatus'],
                 'Condition' => $fullStudies['Study']['ProtocolSection']['ConditionsModule']['ConditionList']['Condition'][0] ?? null,
                 'NCTId' => $fullStudies['Study']['ProtocolSection']['IdentificationModule']['NCTId'],
                 'Organization' => $fullStudies['Study']['ProtocolSection']['IdentificationModule']['Organization']['OrgFullName'],
                 'Title' => $fullStudies['Study']['ProtocolSection']['IdentificationModule']['BriefTitle'],
                 'Sponsor' => $fullStudies['Study']['ProtocolSection']['SponsorCollaboratorsModule']['LeadSponsor']['LeadSponsorName']
            ];
        });

        $studies = $studies->merge($collection);

        $min_rank += $limit;
        $max_rank += $limit;

    } while ($body['FullStudiesResponse']['NStudiesReturned'] > 0 && $min_rank < $max_records);

    return $studies;
}
guybrush_threepwood's avatar

Hi @maracaibo

I think it keeps looping because the API returns more than 200K records in total. Let me update the answer to add an exit condition so it gets only the first 1000.

Regards.

PD: Done

guybrush_threepwood's avatar

That's a part of your original code I haven't touched:

 'Condition' => $fullStudies['Study']['ProtocolSection']['ConditionsModule']['ConditionList']['Condition'][0],

I'm guessing it's not always set on the API response. You could use the null coalesce operator to default to null in that case:

 'Condition' => $fullStudies['Study']['ProtocolSection']['ConditionsModule']['ConditionList']['Condition'][0] ?? null,

Or use implode to convert the array to a comma separated string with all the conditions.

'Condition' => implode(', ', $fullStudies['Study']['ProtocolSection']['ConditionsModule']['ConditionList']['Condition']),
1 like
Maria30's avatar
Level 21

@guybrush_threepwood with this part of my code commented I have the

Undefined offset: 0 

It must be because of this?

 } while ($body['FullStudiesResponse']['NStudiesReturned'] > 0 && $min_rank < $max_records);
guybrush_threepwood's avatar

Mmm, I don't think so, the error refers to an undefined array index (the zero you're refering to is just an integer value).

Are you sure that part of the code is correctly commented out? Could you check the stack trace to see which line is producing the error?

Image

guybrush_threepwood's avatar
Level 33

No problem @maracaibo

Sorry, there was an error on the line that merges de collections.

Instead of: $studies->merge($collection);

It should be: $studies = $studies->merge($collection);

I've updated the original answer.

All the best.

Maria30's avatar
Level 21

@guybrush_threepwood Thank you very much!

This is the way it worked

 public function fullStudies(): Collection
      {
          $collection = collect();


          $client = new Client();

          $min_rank = 1;
          $max_rank = 100;
          $limit = 100;
          $max_records = 1000;

          do {

              $response = $client->get("https://clinicaltrials.gov/api/query/full_studies?min_rnk=$min_rank&max_rnk=$max_rank&fmt=json");

              $body = json_decode((string)$response->getBody(), true);

              $studies = collect($body['FullStudiesResponse']['FullStudies'])->map(function ($fullStudies) {
                  return [
                      'Rank' => $fullStudies['Rank'],
                      'Status' => $fullStudies['Study']['ProtocolSection']['StatusModule']['OverallStatus'],
                      'Condition' => $fullStudies['Study']['ProtocolSection']['ConditionsModule']['ConditionList']['Condition'][0] ?? null,
                      'NCTId' => $fullStudies['Study']['ProtocolSection']['IdentificationModule']['NCTId'],
                      'Organization' => $fullStudies['Study']['ProtocolSection']['IdentificationModule']['Organization']['OrgFullName'],
                      'Title' => $fullStudies['Study']['ProtocolSection']['IdentificationModule']['BriefTitle'],
                      'Sponsor' => $fullStudies['Study']['ProtocolSection']['SponsorCollaboratorsModule']['LeadSponsor']['LeadSponsorName']
                  ];
              });


              $collection = $collection->merge( $studies);

              $min_rank += $limit;
              $max_rank += $limit;

          } while ($body['FullStudiesResponse']['NStudiesReturned'] > 0 && $min_rank < $max_records);
//dd($body['FullStudiesResponse']['NStudiesReturned']);
          return $collection;
      }
1 like

Please or to participate in this conversation.