ChenXiu Posted November 12, 2022 Share Posted November 12, 2022 An API allows me to access information on an unlimited number of SKUs that I submit via cURL. However, their "multiple-lookup" URL limits the SKU numbers to maximum of 5 at a time.Question: Given a list of 100 SKU numbers with a maximum of 5 at a time, what is the best way to loop through them? Their API format is: https://example.com?key=secret&SKU=1234&SKU=4444&SKU=555&SKU=0101&SKU=3333 My PHP script makes the cURL calls, and place the results into a $_SESSION. Here is an example of my code using 7 SKU numbers: $ch = curl_init(); my_curl_function($url_with_SKUs) { curl_setopt($ch, CURLOPT_URL,$url_with_SKUs); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch) $_SESSION["curlResults"][] = $result // I put the results into Session. } $sku = array( '1234', '5678', '4444', '2222', '9393', '1111', '8689' ); $url = 'https://example.com?key=secret'; $append = ''; for($i=0;$i<count($sku);$i++){ $append .= '&SKU='.$sku[$i]; if(($i+1) % 5 == 0) { $url .= $append; $append = ''; my_curl_function($url); } } ...but my code has flaws: • when I use modulus, there are still the final left-over SKU numbers that don't get put in the URL • I'm not even sure I should be using a function at all • Should I even be doing a "for" loop? Or would a "while" loop or a "do" loop be better? Even though this seems to be SUPER basic novice PHP stuff, I can't seem to put this all together in my brain. I've spent weeks on this... Your suggestions will be most welcome. Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/ Share on other sites More sharing options...
mac_gyver Posted November 12, 2022 Share Posted November 12, 2022 i would use array_chunk() to break the starting array into chunks of 5 elements, with the last chunk containing the remainder. you can then simply use a foreach(){} loop to loop over the chunks. rather than concatenating to build the query string, just implode each chunk array. Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602507 Share on other sites More sharing options...
ChenXiu Posted November 12, 2022 Author Share Posted November 12, 2022 (edited) Thank you. When using array_chunk, is this nested "foreach" loop the BEST way to return values, for the API/cURL script I had described: $myArray = array( '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ); $chunked = array_chunk( $myArray , 5 ); foreach($chunked as $key => $value) { foreach ($value as $key1 => $value1) { // $value1 is the result I would want } } Edited November 12, 2022 by ChenXiu Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602509 Share on other sites More sharing options...
Solution mac_gyver Posted November 12, 2022 Solution Share Posted November 12, 2022 $sku = array( '1234', '5678', '4444', '2222', '9393', '1111', '8689' ); foreach(array_chunk($sku,5) as $chunk) { $qs = '&SKU=' . implode('&SKU=',$chunk); // examine the result echo $qs . '<br>'; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602510 Share on other sites More sharing options...
ChenXiu Posted November 12, 2022 Author Share Posted November 12, 2022 Ohhhh okay I see what you meant.Instead of nested foreach loop, the "implode" function does the same thing, but much more efficient! THANK YOU! Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602511 Share on other sites More sharing options...
ChenXiu Posted November 12, 2022 Author Share Posted November 12, 2022 In the example in my previous post, "$sku" was a normal array of SKU numbers. What would I do if the SKU numbers came from a mySQL database? Given the following mySQL query:$result = $db->query("select sku_numbers from table"); Should I put the mySQL result into an array, and then use array_chunk, like this? $result = $db->query("select sku_numbers from table"); while ($each = $result->fetch_assoc()) { $sku[] = $each["sku_numbers"]; } foreach(array_chunk( $sku, 5) as $chunk { // do stuff Or is there a better ("more efficient") mySQL style code to use? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602513 Share on other sites More sharing options...
Barand Posted November 12, 2022 Share Posted November 12, 2022 $result = $db->query("select sku_numbers from table"); $rows = $result->fetchAll(); $sku_array = array_column($rows, 'sku_numbers'); Probably faster using the inbuilt functions. Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602514 Share on other sites More sharing options...
mac_gyver Posted November 12, 2022 Share Posted November 12, 2022 7 minutes ago, ChenXiu said: like this? yes. 7 minutes ago, ChenXiu said: Or is there a better ("more efficient") mySQL style code to use? if you were using the PDO database extension, it has a fetch mode that will directly produce an array of the SELECTed column values. Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602516 Share on other sites More sharing options...
ChenXiu Posted November 12, 2022 Author Share Posted November 12, 2022 @mac_gyver & @Barand: THANK YOU! 😀 Just in this one thread I've learned 3 new things! • array_chunk (very cool!) • using "implode" instead of nested foreach loop! • mySQL "fetchALL" (I didn't know that existed -- I only knew about fetch_obj, fetch_assoc, and fetch_array) Thank you again!! Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602518 Share on other sites More sharing options...
Barand Posted November 12, 2022 Share Posted November 12, 2022 So there is! $result = $db->query("select sku_numbers from table"); $skus = $result->fetchAll(PDO::FETCH_COLUMN); Thanks @mac_gyver - one keeps on learning. Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602519 Share on other sites More sharing options...
Barand Posted November 12, 2022 Share Posted November 12, 2022 3 minutes ago, ChenXiu said: Just in this one thread I've learned 3 new things! 2 new things. fetchAll() is a PDO method. Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602520 Share on other sites More sharing options...
mac_gyver Posted November 12, 2022 Share Posted November 12, 2022 (edited) the msyqli extension does have a fetch_all() function/method, which they finally fixed so that it doesn't depend on the mysqlnd driver, but it is still messed up in that the default fetch mode is numeric, which is different from the default fetch mode for every other general fetch - both/assoc/numeric statement, because the nimrods that programmed this didn't understand that the fetch mode applies to the rows of data, not the all-array holding those rows. Edited November 12, 2022 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602522 Share on other sites More sharing options...
ChenXiu Posted November 12, 2022 Author Share Posted November 12, 2022 Uh oh.... fetchALL didn't work. "error stack trace blah blah blah" @Barand, yes PDO. I don't use PDO. I'm still stuck in PHP circa 1999. (But at least I learned *2* things today 😀) Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602524 Share on other sites More sharing options...
ChenXiu Posted November 12, 2022 Author Share Posted November 12, 2022 May I please ask one final question on this topic?Question: Does this line of code "global $ch;" cause cURL to be inited each time I call this function later in my script? And, if so, can my code be modified so that cURL is only inited once at the beginning of the script? (unneccessarily initing cURL over and over when it only needs to be inited once wastes script execution time): $ch = curl_init(); // I want to only init cURL just once function my_curl_function($url_with_SKUs) { global $ch; curl_setopt($ch, CURLOPT_URL,$url_with_SKUs); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); $decoded = json_decode($result,true); foreach($decoded as $my_results) { // do something } } // some php code here my_curl_function($url); // some php code here my_curl_function($url); // some php code here my_curl_function($url); Quote Link to comment https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/#findComment-1602527 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.