Guest User

Untitled

a guest
Feb 26th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. function jobbank_cron_job_scheduler_info() {
  2. $schedulers = array();
  3. $schedulers['custom'] = array(
  4. 'worker callback' => 'custom_job',
  5. 'jobs' => array(
  6. array('crontab' => '6 * * * *', 'periodic' => TRUE),
  7. )
  8. );
  9. return $schedulers;
  10. }
  11.  
  12.  
  13. function get_job_bank() {
  14. //Save the (long) job bank URL
  15. $jobBankURL = "https://www.jobbank.gc.ca/xmllite/en/?q=city_id:33821";
  16. $jobURL = "https://www.jobbank.gc.ca/xmllite/en/";
  17. $jobs = [];
  18. $jobInfo = null;
  19.  
  20. //Get the XML data into an object
  21. $jobsInfo = simplexml_load_file($jobBankURL);
  22.  
  23. //Send a message and exit if it fails getting the XML
  24. if($jobsInfo === false) {
  25. mail ('webmastercolin@gmail.com','Job Central SM Job Bank CRONjob issue','Getting the $jobsInfo XML data failed (line 9) at '.date('H:i:s on F j, Y'));
  26. exit();
  27. }
  28.  
  29. //Iterate through the objects
  30. foreach($jobsInfo->Documents->Document as $job) {
  31. //Get the job data from Job Bank
  32. $jobInfo = simplexml_load_file($jobURL.$job->jobs_id.".xml");
  33.  
  34. //Grab just the information for the job and put it in the array
  35. $jobs[] = $jobInfo->Documents->Document;
  36. }
  37.  
  38. //Encode the array as JSON
  39. $json = json_encode($jobs);
  40.  
  41. //Send a message and exit if it fails encoding the JSON
  42. if($json === false) {
  43. mail ('webmastercolin@gmail.com','Job Central SM Job Bank CRONjob issue','JSON encoding the array failed (line 23) at '.date('H:i:s on F j, Y'));
  44. exit();
  45. }
  46.  
  47. //Save new data to json file, overwriting existing
  48. $writeResult = file_put_contents($base_path . 'sites/default/files/jobbank/job-bank-data.json',$json,LOCK_EX);
  49.  
  50. //Send a message and exit if overwriting the file failed
  51. if($writeResult === false) {
  52. mail ('webmastercolin@gmail.com','Job Central SM Job Bank CRONjob issue','Writing to the JSON file failed (line 31) at '.date('H:i:s on F j, Y'));
  53. exit();
  54. }
  55.  
  56. //Send a message on success, we are done
  57. mail ('webmastercolin@gmail.com','Job Central SM Job Bank CRONjob Success!','JSON creation succeeded at '.date('H:i:s on F j, Y'));
  58. exit();
  59. }
Add Comment
Please, Sign In to add comment