Jump to content


Json parse from url

  • Please log in to reply
3 replies to this topic

#1 techker

  • Members
  • PipPipPip
  • Advanced Member
  • 750 posts

Posted 06 March 2018 - 02:24 PM

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": [
    "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>';


$obj = json_decode($url2);
print $obj->{'username'};


$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? 


#2 requinix

  • Administrators
  • Maddening Administrator
  • 9,719 posts
  • LocationWA

Posted 06 March 2018 - 04:02 PM

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.
The Reimann Zeta Function Trolley Problem | If you want to escape with me, come take my hand...

#3 Psycho

  • Moderators
  • Move along, nothing to see here
  • 11,909 posts
  • LocationCanada

Posted 06 March 2018 - 04:24 PM

json_decode() is returning an object. Try:


$jsonStr = file_get_contents($url);
$dataObj = json_decode($jsonStr);
echo $dataObj->user_info->username;

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#4 techker

  • Members
  • PipPipPip
  • Advanced Member
  • 750 posts

Posted 06 March 2018 - 04:34 PM

its odd cause im getting an error connection refused..


but when I use postman it get the son data

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users