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

jeans's avatar
Level 1

Related to API (Avoid using forloop)

Hey,

I am fetching data from a third-party API and inserting it into the database. Using two APIs.

  • Product list API
  • Product detail API

I have faced an issue is that when I get data from product list API. It returns me 100 records but on those records, some parameters are not available which I need to use it. So for that purpose, I fetched the required data parameters through detail API by calling inside the for loop.

Like First, I get all product IDs using product list API. After that, I run the for loop and pass those ids in the detail API then make a method for adding those data into the database but this approach is not good. In this approach, I have to hit the API 100 times.

 			 foreach ($getAnimalId as $value) {
                        $animalId = intval($value)'
                        $detailLostAnimal =  $this->detailPetPointAnimal($animalId);
						$this->addAnimals($$detailLostAnimal);
				}

So, Please guide me on how I can get data without using forloop. Below the code

0 likes
15 replies
SilenceBringer's avatar

@jeans it's based on third-party API. If it can't return product details for multiple id at a time - you need to do separate call per each product.

jeans's avatar
Level 1

@SilenceBringer How do I do separate call it on each product? Can you elaborate a little more? I haven't any face as I call it. If it is possible please give me more clarity. Thanks for your response.

Snapey's avatar

can you ask for product detail of multiple items in a single call?

If not then you are stuck with 100 api calls

jeans's avatar
Level 1

@Snapey Yes, I am asking I want to get all product detail through pass product id but without using a loop. I have product ids inside an array.

kokoshneta's avatar

How are we supposed to help you figure out how an API works without even knowing which API it is? There’s no way for us to know how you can do this.

jeans's avatar
Level 1

@kokoshneta I thought you figure it out without knowing the api because I have a query related to record, not api. if you know about the name of that API it's Petpoint API.

I haven't gotten all parameters in petpoint product list API that's why I was called the petpoint detail API.

When I got all the product IDs after I run the loop inside that loop called the detailed API. Then, That data is stored in a particular variable. I hope now you got it very well.

SilenceBringer's avatar

@jeans dude, you question is:

how I can get data without using forloop

Ou question is - does API provide the ability to grab multiple (100 in your case) details at once?

If yes - do it

If no - you need to do 100 separated API calls (one for each), and using loop here is ok.

As you told - we don't know the API, so, study it by yourself and see if you can or not.

jeans's avatar
Level 1

@SilenceBringer API doesn't provide the ability to grab multiple (100 in your case) details at once. So, I have to run forloop for getting the detail per record according to you. right?

SilenceBringer's avatar

@jeans yes. If API force you to do it one-by-one - you need to do it one-by-one, and using loop here is ok.

jeans's avatar
Level 1

@SilenceBringer I did it already in the same way but the issue facing is that sometimes detail API didn't give any response because that API called inside for loop. so I trying to figured out this problem

SilenceBringer's avatar

@jeans hey, if it's your main problem - why it's not described in your original post?

21 hour since starting discussion - and only now you tell what's your problem.

jeans's avatar
Level 1

@SilenceBringer I already described in the main post. Even I also wrote code related to it. look below.

 			 foreach ($getAnimalId as $value) {  //all product ids here
           
                  $detailLostAnimal[] =  $this->detailPetPointAnimal($value);     
		
				}
SilenceBringer's avatar

@jeans

but the issue facing is that sometimes detail API didn't give any response because that API called inside for loop

where it's described in original post?

jeans's avatar
Level 1

@Silence I agreed this content missing in the original post but I apologize for it. I don't try to waste anyone's time. I was shuffling in my issue so I need a supportive answer. So, be a cool dude

Please or to participate in this conversation.