<?php
/*
 * @Author: zp
 * @Date: 2020-04-29 11:36:41
 * @LastEditTime: 2020-05-26 15:33:10
 * @LastEditors: Please set LastEditors
 * @Description: 雷延廷的抓取bokun接口文件
 * @FilePath: \trippest.com\TP\libraries\bokun_lib.php
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Bokun_lib
{
    protected $ci;

    private $bokun_url = 'https://api.bokun.is'; // https://api.bokun.io
    private $access_key = '2f6284e43ee84f6a85fbb0c5660c7404';
    private $secret_key = 'ef3353fe28dc402b9045c2a766b68efe';

    public function __construct()
    {
        $this->ci =& get_instance();
    }

    /**
     * @description: 
     * @param string $activityid :产品的ID
     * @return: json :产品的详细json数据
     * @Date Changed: 
     */
    public function get_activity_detail($activityid){
        $path = "/activity.json/$activityid?currency=USD&lang=EN";
        $activity = $this->call_bokun($path,'get');
        return ($activity);
    }

    public function get_all_product_list()
    {
        $path = "/product-list.json/list?lang=EN";
        $product_list = $this->call_bokun($path,'get');
        return ($product_list);
    }

    public function get_product_list_detail($list_id)
    {
        $path = "/product-list.json/$list_id?currency=USD&lang=EN";
        $product_list = $this->call_bokun($path,'get');
        return ($product_list);
    }

    public function get_activity_price($bokun_id)
    {
        $path = "/activity.json/$bokun_id/price-list?currency=USD";
        $price_list = $this->call_bokun($path,'get');
        return ($price_list);
    }

    private function make_bokun_signature($date, $method, $path)
    {
        $raw_str = $date . $this->access_key . strtoupper($method) . $path;
        return base64_encode(hash_hmac("sha1", $raw_str, $this->secret_key, TRUE));
    }

    private function call_bokun($path, $method, $body=null)
    {
        $url = $this->bokun_url . $path;
        $create_date = gmdate("Y-m-d H:i:s");
        $bokun_signature = $this->make_bokun_signature($create_date, $method, $path);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 20);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER,
            array(
                "X-Bokun-Date:" . $create_date,
                "X-Bokun-AccessKey :" . $this->access_key,
                "X-Bokun-Signature :" . $bokun_signature
            )
        );
        $set_post = strtoupper($method) === 'GET' ? false : true;
        curl_setopt($curl, CURLOPT_POST, $set_post);
        // curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        log_message('debug','call_bokun ' . $method . PHP_EOL . var_export($url, 1));
        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            log_message('error', "curl error code: ".curl_error($curl)."; curl call: ".$path);
        } else {
            $httpStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                log_message('error', "Request html Status Code: ".$httpStatusCode."; curl call: ".$path);
            }
        }
        curl_close($curl);
        return $output;
    }



}

/* End of file Bokun_lib.php */
