使file_get_contents只返回我想要的

时间:2014-06-16 09:17:01

标签: php file-get-contents

我有这个代码。我想在每个位置的aray datte中访问。但我把结果作为字符串。这是例子:

“string(7652)”{“id”:“34”,“nume”:“学生”,“bilet”:null,“log”:“2014-06-16 11:41:28 \ n2014- 06-16 11:41:30 \ n2014-06-16 11:41:41 \ n2014-06-16 11:41:41 \ n2014-06-16 11:41:43 \ n2014-06-16 11:42 :03 \ n2014-06-16 11:42:05 \ n2014-06-16 11:42:06 \ n2014-06-16 11:42:06 \ n2014-06-16 11:42:06 \ n2014-06 -16 11:42:09 \ n2014-06-16 11:42:11 \ n2014-06-16 11:42:“

我只需要日志...所以我可以用日志创建一个heml表。想法?

public $datte = array();
public function aduDpServer(){
    $data = array ('foo' => 'bar', 'id' => 34); 
    $data = http_build_query($data); 
    $context_options = array ( 
        /*'http' => array ( 
            'method' => 'POST', 
            'header'=> "Content-type: application/x-www-form-urlencoded\r\n" 
            . "Content-Length: " . strlen($data) . "\r\n", 
            'content' => $data 
        ) */
        'http'=> array(
            'method'=>'POST',
            'header'=>"Content-type: application/x-www-form-urlencoded\r\n",
            'content'=>$data
        )
    ); 
    $context = stream_context_create($context_options);
    $this->datte = file_get_contents('http://www.jajaja/x/updt.php', false, $context); 

    //var_dump($fp);

}

public function afiseaza(){
    //print_r($this->datte);
    echo"<table border=1 cellpading=0 cellspacing=0 >";
    echo"<th>Accesari</th>";
    $this->datte = is_array($this->datte) ? $this->datte: array($this->datte); //fortare vector
    foreach ($this->datte as $d) {
        echo"<tr>";
        echo"<td>".$d."</td>";echo"<br>";
        echo"</tr>";

    }

}

1 个答案:

答案 0 :(得分:0)

您的字符串是JSON,因此您可以使用json_decode将数据转换为php数组。

$this->datte = json_decode(file_get_contents('http://www.jajaja/x/updt.php', false, $context), true);