techker Posted March 6, 2018 Share Posted March 6, 2018 Hey guy's im calling an api and getting back { "user_info": { "username": "u", "password": "p", "message": "", "auth": 1, "status": "Active", "exp_date": "1521306691", "is_trial": "0", "active_cons": "0", "created_at": "1518887491", "max_connections": "1", "allowed_output_formats": [ "m3u8", "ts", "rtmp" ] }, "server_info": { "url": "server", "port": "25461", "rtmp_port": "8001", "timezone": "Europe/London", "time_now": "2018-03-06 14:05:59" } } im trying to parse in ph to get username and password. I have tried lots of ways but no success? example : $data = json_decode(file_get_contents($url), true); echo "id: ", $data['user_info']['username']; echo '<br>'; echo "Name: ", $data['password']; echo '<br>'; or $obj = json_decode($url2); print $obj->{'username'}; or $jfo = file_get_contents($json_file); // read the title value $title = $jfo->server_info->url; // copy the posts array to a php var $posts = $jfo->user_info->username; no luck?do I have to use like a multilevel son method? Quote Link to comment https://forums.phpfreaks.com/topic/306759-json-parse-from-url/ Share on other sites More sharing options...
requinix Posted March 6, 2018 Share Posted March 6, 2018 The first method should work. If it doesn't then $url is wrong or the returned JSON does not look like what you think it looks like. Dump out $data to see what you're working with. The second won't because json_decode does not accept URLs. It wants a JSON string. Someone didn't read the documentation. The third won't because file_get_contents returns a string. It does not decode JSON. Someone didn't read the documentation. Quote Link to comment https://forums.phpfreaks.com/topic/306759-json-parse-from-url/#findComment-1556961 Share on other sites More sharing options...
Psycho Posted March 6, 2018 Share Posted March 6, 2018 json_decode() is returning an object. Try: $jsonStr = file_get_contents($url); $dataObj = json_decode($jsonStr); echo $dataObj->user_info->username; Quote Link to comment https://forums.phpfreaks.com/topic/306759-json-parse-from-url/#findComment-1556964 Share on other sites More sharing options...
techker Posted March 6, 2018 Author Share Posted March 6, 2018 its odd cause im getting an error connection refused.. but when I use postman it get the son data Quote Link to comment https://forums.phpfreaks.com/topic/306759-json-parse-from-url/#findComment-1556965 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.