我已经创建了一个JSON文件,我将使用它作为应用程序全局配置的数据源。
摘录自json文件
//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" :""
}
]
}
json文件中的callBack_url密钥有一个名称类似的变量,它的值是一个url,例如$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>";
}
}
对于上面的,作为链接的输出,我得到了例如<a href="$fbLoginURL">Continue with facebook</a>
echo "<a href=\"{${$type->callBack_url}}\">{$type->label}</a>";
我没有存储完整的URI的原因是我将动态生成一些参数。
发布于 2018-06-05 04:27:33
请看一下变量手册:http://php.net/manual/en/language.variables.variable.php
基本上,您只需将字符串变量名包装在${ }中,以使其行为类似于实际的变量。
$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};
https://stackoverflow.com/questions/50688143
复制相似问题