Skip to content

Instantly share code, notes, and snippets.

@tranepura
Created July 5, 2016 01:37
Show Gist options
  • Select an option

  • Save tranepura/d901c5eb1b7d90c13232fe8ccdef5302 to your computer and use it in GitHub Desktop.

Select an option

Save tranepura/d901c5eb1b7d90c13232fe8ccdef5302 to your computer and use it in GitHub Desktop.
Insert AC API data from JSON to Drupal tables
$url = 'https://www.ambassadorcard.com.au/api/resync/?return_type=json';
//$url = 'https://www.ambassadorcard.com.au/api/added/?return_type=json&benefit_type=Dining&start_dt=2016-01-01';
//$url = 'https://www.ambassadorcard.com.au/api/removed/?return_type=xml&benefit_type=Dining&start_dt=2016-01-01';
//$url = "https://www.ambassadorcard.com.au/api/updated/?return_type=json&benefit_type=Dining&start_dt=2016-01-01";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$out = curl_exec($ch);
curl_close($ch);
//var_dump(json_decode($out));
$decoded_data = json_decode($out);
try {
$transaction = db_transaction();
//$transaction_2 = db_transaction();
$insert_ac_records = db_insert('ac_sync_data')->fields(array('item_id','date_created','date_updated',
'name','benefit_types','image','short_descr','latitude','longitude'));
foreach ($decoded_data as $value) {
$insert_ac_records
->values(array(
'item_id' => $value->id,
'date_created' => $value->date_created,
'date_updated' => $value->date_updated,
'name' => $value->name,
'benefit_types' => $value->benefit_types,
'image' => $value->img,
'short_descr' => $value->short_descr,
'latitude' => $value->latitude,
'longitude' => $value->longitude
));
//var_dump($value->outlets);
// if(!empty($value->outlets) ) {
$insert_ac_outlets = db_insert('ac_sync_outlets')->fields(array('ac_item_id','outlet_id','name',
'descr','address','suburb','region','postcode','latitude','longitude','phone_1', 'phone_2', 'web', 'state','country','country_code'));
foreach ($value->outlets as $innervalue) {
//var_dump($innervalue);
$phone1='';
if(!empty($innervalue->phone_1)){
$phone =$innervalue->phone_1;
}
$phone2='';
if(!empty($innervalue->phone_1)){
$phone =$innervalue->phone_1;
}
$web='';
if(!empty($innervalue->web)){
$web =$innervalue->web;
}
$insert_ac_outlets
->values(array(
'ac_item_id' => $value->id,
'outlet_id' => $innervalue->outlet_id,
'name' => $innervalue->name,
'descr' => $innervalue->descr,
'address' => $innervalue->address,
'suburb' => $innervalue->suburb,
'region' => $innervalue->region,
'postcode' => $innervalue->post_code,
'latitude' => $innervalue->latitude,
'longitude' => $innervalue->longitude,
'phone_1' =>$phone1,
'phone_2' => $phone2,
'web' => $web,
'state' => $innervalue->state,
'country' => $innervalue->country,
'country_code' => $innervalue->country_code
));
}
$id_outlets = $insert_ac_outlets->execute();
echo '<br><br><br><br>';
}
//var_dump($insert_ac_records);
$id = $insert_ac_records->execute();
echo 'Updated ID :'. $id;
}
catch (Exception $e) {
$transaction->rollback();
//$transaction_2->rollback();
var_dump($e);
watchdog_exception('ac_sync_data', $e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment