2017-04-24 38 views
1

我尝试使用下面的代码来改变CakePHP中3.X的不断值:如何在cakephp中更改常量的值?

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

请帮助解决这个错误:

+2

我认为你缺少一个恒定的角度来看,它应该永远不会改变:http://www.dictionary.com/browse/constant。如果你阅读错误信息,你应该看到PHP正在告诉你这一点。 – drmonkeyninja

+0

我大声问你为什么要改变你的应用程序中的API密钥? –

+0

好的谢谢...我会尝试一些其他的选择,如全局变量那么! – Gagzzz

回答

0

您可以使用全局变量:

var $myVariable; 

//访问此变量

$this->$myVariable;