2017-01-17 69 views
0

我試圖讓這個圖像顯示從一個API調用在PHP中,在HTML中的IMG標記。顯示Api圖像

到目前爲止,這是我嘗試過,

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

但它僅顯示用戶PIC,是不是因爲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

我打算假設你是PHP新手,因爲你打破了一條基本規則。您正在將PHP邏輯代碼與HTML代碼混合在一起。如果您是初學者,並且無法避免將PHP邏輯寫入與HTML代碼相同的文件中,請至少執行以下步驟:先編寫您的PHP代碼,然後在任何HTML之前留下HTML文件末尾的

所以,當然這個$data['response']['avatar']是空的上面,因爲$ data數組是從json_decode中獲取的,但是是「below」。

因此,請在第一件事上添加您的PHP代碼,並讓您的HTML生活在下面。請記住,只因爲你是初學者,你會這樣做。稍後,您將學習像Laravel,Symfony,Yii,CodeIgniter等等的框架......這更像是用於PHP的真實交易