douguang9014 2017-04-24 10:04
浏览 39
已采纳

如何更改cakephp中常量的值?

I am trying to change the value of constant in cakephp 3.x using below code:

    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);
                }       
            } 

But getting below error: enter image description here

Please assist to resolve this error:

  • 写回答

1条回答 默认 最新

  • dongshao2967 2017-04-24 10:57
    关注

    You can use global variables instead like:

    var $myVariable;
    

    // to access this variable

    $this->$myVariable;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我解决一下inode 爆满的问题(有偿)
  • ¥15 关于#vscode#的问题:布料滤波算法中C++实现pcl在Vscode中pcl库没有#include <pcl>
  • ¥15 fpga:ov5640采集tft显示
  • ¥20 创建taro项目,在vscode上能够写跨平台写微信小程序代码
  • ¥15 网站出现错误跳转问题
  • ¥15 Docker容器里已经安装了ssh,但打包迁移到新机器一直容器一直提示unrecognized service。
  • ¥15 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
  • ¥15 使用moviepy库视频合并时出错
  • ¥30 FLUENT液固传质UDF
  • ¥15 怎么看梯度直方图以,怎么判断梯度消失/爆炸,怎么解决