显示Api图像

时间:2017-01-17 22:37:13

标签: php html

我试图让这个图像从php中的api调用显示到html中的img标记。

到目前为止,这是我试过的,

<div class="col-md-3 col-lg-3 " align="center"><img alt="User Pic" <?php echo 'src="'. $data['response']['avatar'] . '"'; ?>

但它只显示用户图片,是因为api不在php内吗?完整代码如下。

<section class="section blog-detail">
    <div class="container">
      <div class="row">
      <div class="col-md-5  toppad  pull-right col-md-offset-3 ">
       <br>
      </div>
        <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xs-offset-0 col-sm-offset-0 col-md-offset-3 col-lg-offset-3 toppad" >


          <div class="panel panel-info">
            <div class="panel-heading">
              <h3 class="panel-title">Profile</h3>
            </div>
            <div class="panel-body">
              <div class="row">

                <div class="col-md-3 col-lg-3 " align="center"> <img alt="User Pic" <?php echo 'src="'. $data['response']['avatar'] . '"'; ?> class="img-circle img-responsive"> </div>
                <div class=" col-md-9 col-lg-9 "> 
                  <table class="table table-user-information">
                    <tbody>
<?php
/* Check if button was clicked */
if (isset($_POST['someName'])) {

    /* If id is empty or not a number, then don't proceed fetching data */
    if (empty($_POST['id']) || !is_numeric($_POST['id'])) {
        echo 'Invalid id';

    } else {

        /* Fetch data */
        $data = file_get_contents('https://api.truckersmp.com/v2/player/' . $id = trim($_POST["id"]));

        /* Decode json */
        $data = json_decode($data, true);

        /* Display id and name */
        if (isset($data['response'])) {
            /* You should add some checks to check if id and name actually exists and are not empty */
            echo '
                <tr><td>ID:</td><td><strong /> ' . $data['response']['id'] . '</td></tr>
                <tr><td>Name:</td><td><strong /> ' . $data['response']['name'] . '</td></tr>
                <tr><td>Join Date:</td><td><strong /> ' . $data['response']['joinDate'] . '</td></tr>
                <tr><td>Steam 64:</td><td><strong /> ' . $data['response']['steamID64'] . '</td></tr>
                <img src="'. $data['response']['avatar'] . '"></td></tr>
                <tr><td>Group:</td><td><strong />' . $data['response']['groupName'].'</td></tr>';   

        }
    }
}
?>  
                    </tbody>
                  </table>

                  <a href="#" class="btn btn-primary">My Sales Performance</a>
                  <a href="#" class="btn btn-primary">Team Sales Performance</a>
                </div>
              </div>
            </div>
                 <div class="panel-footer">
                        <a data-original-title="Broadcast Message" data-toggle="tooltip" type="button" class="btn btn-sm btn-primary"><i class="glyphicon glyphicon-envelope"></i></a>
                        <span class="pull-right">
                            <a data-original-title="Remove this user" data-toggle="tooltip" type="button" class="btn btn-sm btn-danger"><i class="glyphicon glyphicon-remove"></i></a>
                        </span>
                    </div>

          </div>
        </div>
      </div>
    </div>

                    </tbody>
                  </table>

                </div>
              </div>
            </div>

          </div>
        </div>
      </div>
    </div>

1 个答案:

答案 0 :(得分:1)

我会假设你是PHP新手,因为你违反了一条基本规则。您正在将PHP逻辑代码与HTML代码混合在一起。如果您是初学者并且无法避免在与HTML代码相同的文件中编写PHP逻辑,请至少执行以下操作:在任何HTML之前首先编写PHP代码并保留文件的HTML结束

所以,自然这个Data Source=(LocalDB)\v11.0...在上面是空的,因为$ data数组是从json_decode中检索的,但是&#34;在#34;下面。

因此,首先将PHP代码添加到上面,然后将HTML放在下面。请记住,你这样做只是因为你是一个初学者。稍后您将学习像 Laravel,Symfony,Yii,CodeIgniter 等的框架......这更像是PHP的真正的交易