1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Json data t PHP variable

Discussion in 'PHP' started by ShinoRex, Apr 2, 2016.

  1. #1
    Hi all,
    I'm having a json file in a domain and I need to take it's data and print in another domain. How to do it?

    json data can be find here http://beta.qlessticket.com/media.json

    Please help me.
     
    ShinoRex, Apr 2, 2016 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    hdewantara, Apr 2, 2016 IP
  3. fisasti

    fisasti Active Member

    Messages:
    42
    Likes Received:
    5
    Best Answers:
    2
    Trophy Points:
    58
    #3
    Hi Shino. You can use the json_decode php's native function. Then do a var_dump to see what kind of data you have:

    $content = file_get_contents("http://beta.qlessticket.com/media.json");
    $json = json_decode($content);
    var_dump($json);
    Code (markup):
    Hope it helps!
     
    fisasti, Apr 20, 2016 IP
    Vooler and ShinoRex like this.
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #4
    I work with json data all the time and decoding and dumping it is always the first step. I never edit the json directly. I put it into a php variable, make the changes and encode again.
     
    sarahk, Apr 20, 2016 IP
    ShinoRex likes this.
  5. ShinoRex

    ShinoRex Well-Known Member

    Messages:
    227
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    148
    #5
    Vow.. Thanks a lot SarahK.. I did it used this technique. Thanks a million :)
     
    ShinoRex, Apr 20, 2016 IP
  6. ShinoRex

    ShinoRex Well-Known Member

    Messages:
    227
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    148
    #6
    Thanks a lot Fisasti :) I used exactly this function :) , First the error "Cross-Origin Request Blocked" occurred. Then i contacted server people. They fixed the issue :) Is there any other solution for this cross-orgin issue?
     
    ShinoRex, Apr 20, 2016 IP
  7. fisasti

    fisasti Active Member

    Messages:
    42
    Likes Received:
    5
    Best Answers:
    2
    Trophy Points:
    58
    #7
    Good to hear it worked! Look, if you can see the file content from a web-browser, it means the host is accepting external requests, to there is no reason for the Cross-Origin issue. Are you doing this from inside an iframe or loading this content like in a background situation?
     
    fisasti, Apr 21, 2016 IP
  8. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #8
    Are you sure that code work?
    I think it should be something like this:
    
    $a = file_get_contents('http://beta.qlessticket.com/media.json');
    $b = json_decode($a, true);
    $c = $b['results'][0]['content'];
    
    echo $c;
    
    Code (markup):
    I don't need var_dump to see what it is. The data is quite clear though.
     
    ketting00, Apr 21, 2016 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    Technically, that can be condensed quite a bit:
    
    echo json_decode(file_get_contents('http://beta.qlessticket.com/media.json'),true)['results'][0]['content'];
    
    PHP:
     
    PoPSiCLe, Apr 21, 2016 IP
    ketting00 likes this.
  10. ShinoRex

    ShinoRex Well-Known Member

    Messages:
    227
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    148
    #10
    $content = file_get_contents('http://beta.qlessticket.com/media.json');
    $jsonarray = json_decode($content,true);
    print_r($jsonarray);

    This is how I coded. It is working fine :)



     
    ShinoRex, Apr 22, 2016 IP
  11. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #11
    What are you going to do with the array? That is not the way to access the array.

    Are you just going to print out raw data on your site?
     
    ketting00, Apr 22, 2016 IP
  12. ShinoRex

    ShinoRex Well-Known Member

    Messages:
    227
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    148
    #12
    Hey I just used $jsonarray[results] for accessing the array :)

     
    ShinoRex, Apr 22, 2016 IP
  13. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #13
    I see. You are a Wordpress designer :)
     
    ketting00, Apr 22, 2016 IP
  14. sajjadhira

    sajjadhira Greenhorn

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #14
    I am using JSON in my website like thism you can try it,
    $url = 'MY JSON URL';
    $json = file_get_contents($url);
    $json = str_replace("\xEF\xBB\xBF",'',$json); 
    $data = json_decode($json);
    foreach($data as $mydata)
    {
         echo  $mydata->name ;
    echo '<br/>';
         echo  $mydata->views;
    
    }
    PHP:
     
    sajjadhira, May 27, 2016 IP