0

I would like to parse a json variable into another link, below is my code. However, i'v met this error, stating that "file_get_contents(0): failed to open stream: No such file or directory ". The directory is working if I type in manually.

   <?php
        $data = json_decode(file_get_contents('http://movie.com/movieOne?seatNum=' +  $movieSeat));
        $counter = count($data);
         echo "<script>movieBTN(x,y){ if (y == 'yes') { alert(x + 'Book Sucessful'); else { alert (x + 'Not sucessful');}</script>";

         for ($x = 0; $x < $counter; $x++) {
              echo  $data[$x]->avail . "<br>" ;
              $avail = $data[$x]->avail;
              echo "<button onclick = movieBTN('".$movieSeat.", ".$avail."')> Movie Seat </button>";
     }

        ?>

the $movieSeat is gotten from another json.

After I change from + to . I have this error "Undefined offset: 0" and "Trying to get property of non-object"

vardump

array(1) { ["avail"]=> string(3) "yes" } 
6
  • Where do you type it manually? Browser? Commented May 25, 2017 at 20:26
  • yes, in the browser.. I have also tried it in the code as well, like change the change the link to (movie.com/movieOne?seatNum=1A) . Both works fine. However when I try to get the variable from another json , it doesn't work. Having the error as stated. Commented May 25, 2017 at 20:30
  • You do realize that browser sends bunch of headers when it sends response to server. And file_get_contents function dies not. My guess those guys defend from bots... Commented May 25, 2017 at 20:32
  • Use var_dump($movieSeat); before $data = json_decode(...); and see if you're getting the variable you expect. Commented May 25, 2017 at 20:33
  • 1
    concatenation in PHP is done with a dot . not a plus + Commented May 25, 2017 at 20:36

1 Answer 1

2

Concatenation in PHP is done with a dot . not a plus +

so change this line to

$data = json_decode(file_get_contents('http://movie.com/movieOne?seatNum=' . $movieSeat));
// -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  ^

Then just use foreach:

foreach($data as $v) {
    echo  $v['avail'];
}
Sign up to request clarification or add additional context in comments.

7 Comments

why do I have error such as "Undefined offset: 0" and "trying to get property of non-object" when I change to .
Show us a var_export($data); please. Edit that into your question
string(2) "yes" makes no sense? "yes" is a 3 character string
Sorry, copied wrongly. I copied the "no".. I've changed
I see the edit. A copy/paste of a var_dump is safer than screwing it up
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.