I've been making some code to loop through until a certain amount of data has been retrieved from an API, incrementing the date used by 1 each time. I've encountered an error which I can't seem to fix, where I get the error: Object of class DateTime could not be converted to string
As far as I know, the format function on a DateTime object should and does always return a string, but even after formatting it, it says that it isn't. This is my code:
$stmt = $conn->prepare("INSERT INTO `orders` (`orderReference`,`assignedAocId`,`dropDate`) VALUES(?,?,?) ON DUPLICATE KEY UPDATE `assignedAocId`=VALUES(`assignedAocId`), `dropDate`=VALUES(`dropDate`)");
$stmt->bind_param('sis',$order_reference,$aoc_id,$date);
while(count($aoc_ids) > 0) {
// $date = date("d/m/Y");
$date = new DateTime();
$date->modify("-30 days");
$aoc_id = $aoc_ids[0];
$run_count = 0;
$populated_count = 0;
while($populated_count < 2 && $run_count < 5) {
$date = $date->modify("+1 day");
$formatted_date = $date->format("d/m/Y"); // Keeps looping through and incrementing date but never gets data
$session_id = generate_session_id($ch);
curl_setopt($ch, CURLOPT_URL, "https://live3.maxoptra.com/rest/2/distribution-api/orders/getOrdersWithZone");
curl_setopt($ch, CURLOPT_POSTFIELDS,
"sessionID={$session_id}&date={$formatted_date}&aocID={$aoc_id}");
if(($xml = new SimpleXMLElement(curl_exec($ch))) !== false) {
if($xml->error->errorCode != 1016) {
if (sizeof($xml->OrdersWithZoneResponse->orders) > 0) {
foreach ($xml->OrdersWithZoneResponse->orders->order as $order) {
$order_reference = (string)$order["referenceNumber"];
$stmt->execute(); // Throws the error on this line
}
$populated_count = $populated_count+1;
}
$run_count = $run_count+1;
} elseif($xml->error->errorCode == 1016) {
handle_error("getOrdersWithZone Invocation Limit Reached");
} else {
handle_error("getOrdersWithZone Unknown Error Code Returned",$debug_email);
exit();
}
} else {
handle_error("getOrdersWithZone cURL Failed",$debug_email);
exit();
}
}
array_shift($aoc_ids);
}