File: /var/www/panigaletech.com/wp-content/themes/panigale/template-parts/get_alljob_and_save.php
<?php
/**
* Template Name: Get All Job And Save
*
* @package WordPress
* @subpackage Twenty_Fourteen
* @since Twenty Fourteen 1.0
*/
global $wpdb;
$client_secret = 'MpZixnLHV6sAFGZUA8M0QwJw';
$client_id = 'edf1089c-c6a7-43a1-a6ed-7f85c0cd7fe2';
$table_name = "bullhorn_token";
$token_details = $wpdb->get_results( "SELECT * FROM $table_name ORDER BY i_id DESC LIMIT 1" );
/**** Get Access Token From Refresh Token start ****/
$refresh_token = $token_details[0]->s_refresh_token;
$token_url = 'https://auth.bullhornstaffing.com/oauth/token?grant_type=refresh_token&refresh_token='.$refresh_token.'&client_id='.$client_id.'&client_secret='.$client_secret.'';
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST, '1');
//execute post
$token_result = curl_exec($ch);
//close connection
curl_close($ch);
$token_result_arr = json_decode($token_result);
$access_token = $token_result_arr->access_token;
$new_refresh_token = $token_result_arr->refresh_token;
//print_r($token_result_arr);
$table = 'bullhorn_token';
$data = array('s_access_token' => $access_token, 's_refresh_token' => $new_refresh_token,'dt_created_date'=>date('Y-m-d H:i:s'));
$wpdb->insert($table,$data);
unset($ch);
/**** Get Access Token From Refresh Token end ****/
/**** Create BHRestToken start ****/
$bhrest_token_url = 'https://rest.bullhornstaffing.com/rest-services/login?version=*&access_token='.$access_token.'';
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch,CURLOPT_URL, $bhrest_token_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//execute post
$bhrest_token_result = curl_exec($ch);
//close connection
curl_close($ch);
$bhrest_token_result_arr = json_decode($bhrest_token_result);
$bhrest_token = $bhrest_token_result_arr->BhRestToken;
$rest_url = $bhrest_token_result_arr->restUrl;
unset($ch);
/**** Create BHRestToken end ****/
/**** Get All Job start ****/
$job_api_url = $rest_url.'search/JobOrder?query=isDeleted:0&BhRestToken='.$bhrest_token.'&fields=*&start=0&count=500';
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch,CURLOPT_URL, $job_api_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//execute post
$job_result = curl_exec($ch);
//close connection
curl_close($ch);
$job_result_arr = json_decode($job_result);
echo '<pre>';print_r($job_result_arr);echo '<pre>';exit;
/**** Get All Job end ****/
$total_jobs = $job_result_arr->total;
if($total_jobs >0)
{
$all_job_data = $job_result_arr->data;
foreach($all_job_data as $all_job_data_val)
{
$job_id = $all_job_data_val->id;
$full_address = $all_job_data_val->address;
$address1 = $full_address->address1;
$city = $full_address->city;
$state = $full_address->state;
$zip = $full_address->zip;
$country_id = $full_address->countryID;
$clientContact = $all_job_data_val->clientContact->firstName.' '.$all_job_data_val->clientContact->lastName;
$clientCorporation = $all_job_data_val->clientCorporation->name;
$date_added = $all_job_data_val->dateAdded;
$description = $all_job_data_val->description;
$employmentType = $all_job_data_val->employmentType;
$numOpenings = $all_job_data_val->numOpenings;
$payRate = $all_job_data_val->payRate;
$publicDescription = $all_job_data_val->publicDescription;
$status = $all_job_data_val->status;
$title = $all_job_data_val->title;
$yearsRequired = $all_job_data_val->yearsRequired;
$owner_name = $all_job_data_val->owner->firstName.' '.$all_job_data_val->owner->lastName;
$my_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'jobs'
);
//print_r($my_post);
// Insert the post into the database
$wp_job_id = wp_insert_post( $my_post );
add_post_meta( $wp_job_id, 'id', $job_id, true );
add_post_meta( $wp_job_id, 'address', $address1, true );
add_post_meta( $wp_job_id, 'employment_type', $employmentType, true );
add_post_meta( $wp_job_id, 'owner', $owner_name, true );
add_post_meta( $wp_job_id, 'payrate', $payRate, true );
add_post_meta( $wp_job_id, 'years_required', $yearsRequired, true );
add_post_meta( $wp_job_id, 'client_contact', $clientContact, true );
add_post_meta( $wp_job_id, 'client_corporation', $clientCorporation, true );
add_post_meta( $wp_job_id, 'description', $description, true );
add_post_meta( $wp_job_id, 'public_description', $publicDescription, true );
add_post_meta( $wp_job_id, 'num_of_openings', $numOpenings, true );
add_post_meta( $wp_job_id, 'job_status', $status, true );
add_post_meta( $wp_job_id, 'address_city', $city, true );
add_post_meta( $wp_job_id, 'address_state', $state, true );
add_post_meta( $wp_job_id, 'address_zip', $zip, true );
add_post_meta( $wp_job_id, 'address_country', $country_id, true );
add_post_meta( $wp_job_id, 'date_added', $date_added, true );
add_post_meta( $wp_job_id, 'full_data', $all_job_data_val, true );
unset($my_post);
}
}
?>