将Json Variable解析为链接

时间:2017-05-25 20:23:56

标签: php json

我想将一个json变量解析成另一个链接,下面是我的代码。 但是,我遇到了这个错误,说明"file_get_contents(0): failed to open stream: No such file or directory "。如果我手动键入该目录。

   <?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>";
     }

        ?>

$movieSeat来自另一个json。

+更改为.后 我有这个错误"Undefined offset: 0""Trying to get property of non-object"

vardump

array(1) { ["avail"]=> string(3) "yes" } 

1 个答案:

答案 0 :(得分:2)

PHP中的连接是使用点.而非加号+

完成的

所以将此行更改为

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

然后使用foreach

foreach($data as $v) {
    echo  $v['avail'];
}