0

I'm having some trouble getting a cache of an API I'm working with.

The code below works perfectly fine when the $cache->$do_url is just a file without the array for the ID and KEY, but the problem I'm having is I have to pass the API ID and Key in order for the API to spit out the info I need.

namespace Gilbitron\Util;
class SimpleCache {

    // Path to cache folder (with trailing /)
    public $cache_path = 'cache/';
    // Length of time to cache a file (in seconds)
    public $cache_time = 86400;
    // Cache file extension
    public $cache_extension = '.cache';

    // This is just a functionality wrapper function
    public function get_data($label, $url)
    {
        if($data = $this->get_cache($label)){
            return $data;
        } else {
            $data = $this->do_curl($url);
            $this->set_cache($label, $data);
            return $data;
        }
    }

    public function set_cache($label, $data)
    {
        file_put_contents($this->cache_path . $this->safe_filename($label) . $this->cache_extension, $data);
    }

    public function get_cache($label)
    {
        if($this->is_cached($label)){
            $filename = $this->cache_path . $this->safe_filename($label) . $this->cache_extension;
            return file_get_contents($filename);
        }

        return false;
    }

    public function is_cached($label)
    {
        $filename = $this->cache_path . $this->safe_filename($label) . $this->cache_extension;

        if(file_exists($filename) && (filemtime($filename) + $this->cache_time >= time())) return true;

        return false;
    }

    //Helper function for retrieving data from url
    public function do_curl($url)
    {
        if(function_exists("curl_init")){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            $content = curl_exec($ch);
            curl_close($ch);
            return $content;
        } else {
            return file_get_contents($url);
        }
    }

    //Helper function to validate filenames
    private function safe_filename($filename)
    {
        return preg_replace('/[^0-9a-z\.\_\-]/i','', strtolower($filename));
    }
}

$cache = new SimpleCache();
$cache->cache_path = 'cache/';
$cache->cache_time = 86400;

if($data = $cache->get_cache('label')){
    $data = json_decode($data);
} else {
    $data = $cache->do_curl( 'http://data.leafly.com/locations/denver-relief/reviews?skip=0&take=12',
                array(
                    'headers' => array(
                        'app_id' => 'MYIDHERE',
                        'app_key' => 'MYKEYHERE'
                    )
                )
            );
    $cache->set_cache('label', $data);
    $data = json_decode($data);
}

print_r($data);

Any ideas?

EDIT WITH ANSWER

For anyone looking through Google for a similar issue, I found the answer.

I changed the do_curl line that pulls the API link to this:

$data = $cache->do_curl( 'http://data.leafly.com/locations/denver-relief/reviews?skip=0&take=12' );

and added the following line in the do_curl function to pull in the header app_id and app_key info needed:

curl_setopt($ch, CURLOPT_HTTPHEADER,array('app_id: MYAPPID','app_key: MYAPPKEY'));
  • Congratulations! Seems like you found your solution just seconds before I had typed it :) Still have a look though, as it contains a suggestion on how to make your curl request more dynamic by passing the headers as variable – T3 H40 Oct 08 '15 at 18:18
  • @T3H40 thanks! yeah, I was doing some searching after I posted and found it. Going to mark your answer as the correct one though since you gave the right answer. – Robert DeVore Oct 08 '15 at 19:27
  • thanks a bunch! Glad I (could have) helped :) – T3 H40 Oct 08 '15 at 19:28

1 Answers1

2

Your public function do_curl($url) has only one parameter. In order to pass your headers, you need to add a parameter like this: public function do_curl($url, $header).

You can then add these headers using something like the following code: curl_setopt($ch, CURLOPT_HTTPHEADER, $header); Your method call should be like:

$data = $cache->do_curl( 
     'http://data.leafly.com/locations/denver-relief/reviews?skip=0&take=12',
     array('app_id: MYIDHERE', 'app_key: MYKEYHERE')
);

Have a look here for reference.

Also have a look at the documentation.

Community
  • 1
  • 1
T3 H40
  • 2,326
  • 8
  • 32
  • 43