How to Get Value this Json with Php ?

Discussion in 'PHP' started by pogung177, Apr 4, 2013.

  1. #1
    https://www.googleapis.com/freebase/v1/mqlread/?lang=/lang/en&query=[{%20%22id%22:%20null,%20%22name%22:%20null,%20%22id|=%22:%20[%20%22/m/01w7nww%22,%22/m/01fm07%22,%22/m/022c7z%22,%22/m/0gywn%22%20]%20}]

    Json Result.
    I would like to print all Name values.
    Thanks
     
    pogung177, Apr 4, 2013 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    EricBruggema, Apr 4, 2013 IP
  3. pogung177

    pogung177 Well-Known Member

    Messages:
    415
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    110
    #3
    I have tried with file_get_contents / curl then json_decode it but get blank value or null
     
    pogung177, Apr 5, 2013 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    Then you have a problem somewhere

    try adding error_reporting(E_ALL) at the top of your code (after <?php)

    I've tried this
    
     
    <pre>
    <?php
    // extension=php_openssl.dll
    $url = 'https://www.googleapis.com/freebase/v1/mqlread/?lang=/lang/en&query=[{%20%22id%22:%20null,%20%22name%22:%20null,%20%22id|=%22:%20[%20%22/m/01w7nww%22,%22/m/01fm07%22,%22/m/022c7z%22,%22/m/0gywn%22%20]%20}]';
    print_r(json_decode(file_get_contents($url)));
    ?>
    
    Code (markup):

    and the result is

    
    stdClass Object
    (
        [result] => Array
            (
                [0] => stdClass Object
                    (
                        [name] => Soul music
                        [id] => /m/0gywn
                    )
     
                [1] => stdClass Object
                    (
                        [name] => Neo soul
                        [id] => /m/01fm07
                    )
     
                [2] => stdClass Object
                    (
                        [name] => Beat
                        [id] => /m/022c7z
                    )
     
                [3] => stdClass Object
                    (
                        [name] => Erykah Badu
                        [id] => /m/01w7nww
                    )
     
            )
     
    )
    Code (markup):
    So this should work! and if you want to print the names you can loop the array


    for example:
    
    <?php
    $url = 'https://www.googleapis.com/freebase/v1/mqlread/?lang=/lang/en&query=[{%20%22id%22:%20null,%20%22name%22:%20null,%20%22id|=%22:%20[%20%22/m/01w7nww%22,%22/m/01fm07%22,%22/m/022c7z%22,%22/m/0gywn%22%20]%20}]';
    $code = json_decode(file_get_contents($url));
     
    if (isset($code['result']) && count($code['result']) > 0)
    {
        foreach ($code['result'] AS $k=>$v)
        {
            echo $v['name'] . "<br />";
        }
    }
    ?>
    
    Code (markup):
     
    EricBruggema, Apr 5, 2013 IP
  5. pogung177

    pogung177 Well-Known Member

    Messages:
    415
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    110
    #5
    many thanks, The problem is in urlencode the url
     
    pogung177, Apr 5, 2013 IP