0

I have created a JSON file which I will use as a datasource for global configurations of the app.

Excerpt from json file

 //Have not put the complete json file. No error in the file
 {
 "loginType":[
    {
        "name":"Facebook",
        "url":"#",
        "method":"",
        "label":"Continue with Facebook",
        "type":"social",
        "class":"",
        "icon":"",
        "callBack_url" : "fbLoginUrl",
        "providerButton":"<div class='fb-login-button' data-max-rows='1' 
        data-size='large' data-button-type='continue_with' data-use- 
        continue-as='true'></div>"        
    },
    {
        "name":"Twitter",
        "url":"#",
        "method":"logInWithTwitter()",
        "label":"Continue with Twitter",
        "type":"social",
        "class":"",
        "icon":"",
        "callBack_url" : "twitterLoginUrl",
        "providerButton" :""
      }
    ]
}

The callBack_url key in the json file has a variable with a similar name which has a url as its value e.g $twitterLoginUrl = "https://some.site.com/twitter_login?param1"

$jsonData_signIn =json_decode
(file_get_contents(/path/to/oauth2_provider.json)); 
 $oauth2Provider = jsonData_signIn->loginType;
       foreach($oauth2Provider as $type){
         if($type->type == 'local' ){
           echo "<a href=\"{$type->callBack_url}\">{$type->label}</a>";
         }
    }    

For the above, as output for the link I get eg <a href="$fbLoginURL">Continue with facebook</a>

echo "<a href=\"{${$type->callBack_url}}\">{$type->label}</a>";

The reason I am not storing the complete URI is I will generate some parameters dynamically.

5
  • 4
    Do not do that. Do not use variable variables. Anywhere you use variable variables you should be using associative arrays instead. Commented Jun 4, 2018 at 20:20
  • ... making a variable name out of an url no less... Yikes ;) Commented Jun 4, 2018 at 20:28
  • 2
    I put an actual answer to your question below but I 100% agree with @Sammitch in that you shouldn't be using variable variables. Just because PHP lets you be sloppy doesn't mean you should. Think about what happens if I put 'mysql_password' into the JSON... Commented Jun 4, 2018 at 20:35
  • Thanks Sammitch, I will use associative arrays. I wanted one file which for global configs.Associative arrays should do the job. Commented Jun 5, 2018 at 6:40
  • @IncredibleHat the url is stored in a variable. The json file contains the name of the variable. The solution should convert the string contained in the value of that key to a variable. Commented Jun 5, 2018 at 6:55

1 Answer 1

2

Take a look at the manual for variable variables: http://php.net/manual/en/language.variables.variable.php

You basically just wrap the string variable name in ${ } to make it behave like an actual variable.

$fbLoginUrl = 'https://www.facebook.com/v2.10/dialog/oauth?client_id=xxxxxxx&state=xxxxxxx&response_type=code&sdk=php-sdk-5.6.2&redirect_uri=some.site.com/fbLogin.php&scope=public_profile';
$json = '{"callBack_url" : "fbLoginUrl"}';
$decoded = json_decode($json);
echo ${$decoded->callBack_url};
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It works. The error I was getting was due to a blank field in my json file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.