dimanche 28 juin 2015

PHP prematurely exiting while loop

Pretty new to PHP and I am stuck on a (what I consider) a strange issue. I have spliced this file together, (it was split across several different functions) for testing, and to make it easier to explain the issue.

This is a basic while loop in Laravel and it seems to be exiting prematurely but oddly not exiting out to the line AFTER the loop, but exiting out BEFORE the loop and then entering again. Can't for the life of me workout why. I have added some log events throughout the function so I could try understand what was happening.

This is correctly fetching and writting products to the DB up until page 7 and then I get a "Start API Helper" event on log but never get an "End API helper". So somewhere in page 7 something is causing the while loop to exit out to the lines above, reset the pagecount to 0. then re-enter the loop, fetching the first lot of products and throwing an SQL duplicate key exception when writing. I know it is exiting out to before the loop as I get a "Before while", right after "New Client" in the log. And of course, the pagecount is resetting. How can this happen?

Any help would be sooooo greatly appreciated. Sorry the formatting of the PHP got a bit lost in translation.

public function store()
{

    $items = array();

    Log::info('Before while');

    $pagecount = 0;
    $prodcount = 1;

     while ($prodcount > 0) {

        Log::info('Top of while');

        $prodcount = 0; // Reset product counter

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

            // JSON is returning an empty array for empty strings. Need to convert to empty string for SQL save.
            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';

}

Aucun commentaire:

Enregistrer un commentaire