如何更改cakephp中常量的值?

时间:2017-04-24 10:04:29

标签: php constants

我正在尝试使用以下代码更改cakephp 3.x中常量的值:

    public function switchApiKey(){
                **/*** changing value of already defined key***/
define('GOOGLE_API_KEY','AIzaSyCzFMBxvOXiHs8DdYoXDwsgcNxtyIhPUBk');**
                $this->loadModel('ApiKeys');
                $current_api_key=GOOGLE_API_KEY;
                $youtube_api_url = "https://www.googleapis.com/youtube/v3/search?key=AIzaSyB2Dolp4pbvHLwwKLl_mT8GWsByy1Hyijk";
                $youtube_api_url = $youtube_api_url."&regionCode=GB&type=video&maxResults=50&part=snippet&q=".urlencode($track->name.' Music Video Vevo');
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_URL,$youtube_api_url);
                $result=curl_exec($ch);
                $response = json_decode($result, true);
                if($response['error']['code']==403){
                    /** Make previous key inactive **/
                    $query = $this->YoutubeChannels->query();

                    $query->update()
                        ->set(['status' => 'pending'])
                        ->where(['api_key' => GOOGLE_API_KEY])
                        ->execute();
                    /** Retrieving fresh key from database **/
                    $new_key=$this->ApiKeys->find()->where(['ApiKeys.status'=>'active'])->first();
                    echo $new_key; die;
                    define(GOOGLE_API_KEY,$new_key->api_key);
                }       
            } 

但是低于错误: enter image description here

请协助解决此错误:

1 个答案:

答案 0 :(得分:0)

您可以使用全局变量,例如:

var $myVariable;

//访问此变量

$this->$myVariable;
相关问题