Pretty new to PHP, and I'm stuck on (what I think) a weird problem. I combined this file together (it was divided into several different functions) for testing and made it easier to explain the problem.
This is the base while loop in Laravel, and it seems to exit prematurely, but, oddly enough, does not exit the line after the cycle, but exits the cycle and then reinserts it. Why don't I do life? I added some log events in the whole function so that I can understand what is happening.
This is the correct selection and writing of products in the database until page 7, and then I get the "Run API Assistant" event in the log, but I never get the "End API Helper". Therefore, somewhere on page 7, something causes the while loop to exit the lines above, reset pagecount to 0. then re-enter the loop, selecting the first batch of products and throwing an SQL-duplicating key exception when writing. I know that it crashes before the cycle, when I get "Before I", right after the "New Client" in the magazine. And of course, the pagecount is reset. How can this happen?
Any help would be so appreciated.
public function store()
{
$items = array();
Log::info('Before while');
$pagecount = 0;
$prodcount = 1;
while ($prodcount > 0) {
Log::info('Top of while');
$prodcount = 0;
Log::info('Page Count:'.$pagecount);
Log::info('Start API Helper');
$headers = array(
'NETOAPI_KEY' => env('NETO_API_KEY'),
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'NETOAPI_ACTION' => 'GetItem'
);
Log::info('Headers Declared');
$body = '{
"Filter": {
"DateAddedFrom" : "2013-06-01 12:00:00",
"Page": "'.$pagecount.'",
"Limit": "500",
"OutputSelector": [
**Lots of JSON here - removed for readability**
]
}
}';
Log::info('Start API Helper');
$client = new client();
Log::info('New Client');
try {
$res = $client->post(env('NETO_API_URL'), [ 'headers' => $headers , 'body' => $body ]);
} catch (RequestException $e) {
echo $e->getRequest();
Log::error($e->getRequest());
if ($e->hasResponse()) {
echo $e->getResponse();
Log::error($e->getResponse());
}
}
Log::info('End API Helper');
$items = json_decode(utf8_encode($res->getBody()), true);
foreach($items["Item"] as $item)
{
foreach($item as $key => $value){
if (empty($value)) {
$item["$key"] = "";
}
}
$Product = new Item;
$Product->SKU = array_get($item, 'SKU');
** LOTS OF DB FIELDS REMOVED FROM HERE FOR READABILITY**
$Product->save();
$prodcount++;
}
$pagecount++;
Log::info($prodcount.' products written to DB:');
Log::info('Bottom of while');
};
Log::info('Exited While');
return 'Complete';
}
: ELoquent SQL firstorNew, . "while exit" . 7 ( . 8), 0, , 2 . , , 3 , 4. . .
local.INFO: Page Count:1
local.INFO: Page Count:2
local.INFO: Page Count:3
local.INFO: Page Count:4
local.INFO: Page Count:5
local.INFO: Page Count:6
local.INFO: Page Count:7
local.INFO: Page Count:8
local.INFO: Page Count:0
local.INFO: Page Count:9
local.INFO: Page Count:1
local.INFO: Page Count:10
local.INFO: Page Count:2
local.INFO: Page Count:11
local.INFO: Page Count:3
local.INFO: Page Count:12
... ...
, API JSON? , 25 , , ?