Jump to content

PHP Loop: API with max queries


Go to solution Solved by mac_gyver,

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/315525-php-loop-api-with-max-queries/
Share on other sites

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.

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 by ChenXiu

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!

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.

 @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!!

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 by mac_gyver

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);

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.