php - Refactoring smartrecruiter api -


i'm implementing smartrecruiter api in project. i'm using 2 endpoints namely /jobs-list , /job-details. problem every time i'm extracting details in second endpoint /job-details, execution time slow.

enter image description here

here's i've done far:

function getcontext()  {     $opts = array(       'http'=> array(             'method' => 'get',             'header' => 'x-smarttoken: xxxxxxxxxxxxxxxxx'              )     );     return $context = stream_context_create($opts);  }  function getsmartrecruitmentjob($city, $department)  {     $tmp    = array();     $results= array();     $limit  = 100; //max limit smartrecruiter api 100      // open file using http headers set above     $file = file_get_contents('https://api.smartrecruiters.com/jobs?limit='.$limit.'&city='.$city.'&department='.$department, false, $this->getcontext());     $lists= json_decode($file, true);      foreach($lists['content'] $key => $list)      {         if ($list['status'] == 'sourcing' || $list['status'] == 'interview' || $list['status'] == 'offer')          {             $results['id'] = $list['id'];             $tmp[] = $this->getsmartrecruitmentjobdetails($results['id']);           }     }     return $tmp; }   function getsmartrecruitmentjobdetails($id)  {     $results  = array();     $file = file_get_contents('https://api.smartrecruiters.com/jobs/'.$id, false, $this->getcontext());     $lists= json_decode($file, true);      $results['title']            = isset($lists['title']) ? $lists['title'] : null;     $results['department_label'] = isset($lists['department']['label']) ? $lists['department']['label'] : null;     $results['country_code']     = isset($lists['location']['countrycode']) ? $lists['location']['countrycode'] : null;     $results['city']             = isset($lists['location']['city']) ? $lists['location']['city'] : null;     $results['url']              = isset($lists['actions']['applyonweb']['url']) ? $lists['actions']['applyonweb']['url'] : null;     return $results; } 

thank in advance.

solved caching function extracting data:

function getcache()  {     if ($this->cache === null)      {         $cache = \zend\cache\storagefactory::factory(          array(           'adapter' => array(            'name' => 'filesystem',            'options' => array(             'ttl' => 3600 * 7, // 7 hours             'namespace' => 'some-namespace',             'cache_dir' => 'your/cache/directory'            ),           ),           'plugins' => array(            'clear_expired_by_factor' => array('clearing_factor' => 10),           ),          )         );            $this->cache = $cache;     }     return $this->cache; }  function getsmartrecruitmentjobdetails($id)  {      $cache = $this->getcache();     $key = md5('https://api.smartrecruiters.com/jobs/'.$id);     $lists = unserialize($cache->getitem($key, $success));     $results  = array();      if($success && $lists)      {         header('debug-cache-recruit: true');     }     else      {         header('debug-cache-recruit: false');         // open file using http headers set above         $file = file_get_contents('https://api.smartrecruiters.com/jobs/'.$id, false, $this->getcontext());         $lists= json_decode($file, true);         $cache->additem($key, serialize($lists));        }      $results['title']            = isset($lists['title']) ? $lists['title'] : null;     $results['department_label'] = isset($lists['department']['label']) ? $lists['department']['label'] : null;     $results['country']      = isset($lists['location']['country']) ? $lists['location']['country'] : null;     $results['country_code']     = isset($lists['location']['countrycode']) ? $lists['location']['countrycode'] : null;     $results['city']             = isset($lists['location']['city']) ? $lists['location']['city'] : null;     $results['url']              = isset($lists['actions']['applyonweb']['url']) ? $lists['actions']['applyonweb']['url'] : null;     return $results; } 

Comments