Created
April 14, 2014 04:49
-
-
Save sparkweb/10616845 to your computer and use it in GitHub Desktop.
Refeed Failed FoxyCart Datafeeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Author: David Hollander, www.sparkweb.net | |
//Recommendation: install this script on your server and setup a cron to run it every few hours. | |
//Set Config Details | |
$domain = ""; //yourstore.foxycart.com | |
$apikey = ""; | |
$days_to_check = 7; | |
//Make sure we are set up | |
if (!$domain || $apikey) { | |
die("Please set config details"); | |
} | |
//Check For Transactions in Last X Days | |
$args = array( | |
"api_action" => "transaction_list", | |
'hide_transaction_filter' => "0", | |
'transaction_date_filter_begin' => Date("Y-m-d", strtotime("-{$days_to_check} days")), | |
'transaction_date_filter_end' => Date("Y-m-d"), | |
"data_is_fed_filter" => 0, | |
); | |
$foxy_data = get_foxycart_data($args); | |
$xml = simplexml_load_string($foxy_data, NULL, LIBXML_NOCDATA); | |
//Nothing Found | |
if (!isset($xml->transactions)) { | |
die("No Transactions Found"); | |
} | |
//Loop Through All Returns | |
foreach ($xml->transactions->transaction as $transaction) { | |
//Resend to Datafeed | |
$transaction_id = (int)$transaction->id; | |
$args = array( | |
"api_action" => "transaction_datafeed", | |
"transaction_id" => $transaction_id, | |
); | |
get_foxycart_data($args); | |
echo "Transaction #" . $transaction_id . " Has Been Resent to Datafeed Endpoints<br>"; | |
} | |
//Access the FoxyCart API | |
function get_foxycart_data($foxy_data, $silent_fail = true) { | |
global $domain, $apikey; | |
$foxy_data = array_merge(array("api_token" => $apikey), $foxy_data); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://" . $domain . "/api"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $foxy_data); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); | |
$response = trim(curl_exec($ch)); | |
if (!$response) { | |
if ($silent_fail) { | |
$response = "<?xml version='1.0' encoding='UTF-8'?><foxydata><result>ERROR</result><messages><message>Connection Error: " . curl_error($ch) . "</message></messages></foxydata>"; | |
} else { | |
die("Connection Error: " . curl_error($ch)); | |
} | |
} | |
curl_close($ch); | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment