簡體   English   中英

給定圖像的URL,如何在PHP中顯示圖像? (PHP NOOB)

[英]How to display an image in PHP given the url of the image? (PHP NOOB)

因此,我正在使用Twitter RestAPI獲取用戶圖片,並能夠在本地頁面上顯示圖片。 我已經找到了獲取圖像URL的方法,這就是我到目前為止的方法:

   $url = $result->profile_image_url;

這保存了用戶個人資料圖片的網址。 現在如何使用該URL在頁面上顯示它? 我知道HTML允許我們做類似的事情:

   <img src = "some url">

它將在網頁上顯示圖片。 我已經在互聯網上瀏覽了一段時間,但仍然沒有弄清楚。 我遇到了以下相同的建議:

   <img src = "<?php echo ($url); ?>"/>

但是,當我嘗試加載頁面時,我一直收到解析錯誤。 如果有人可以引導我/幫助我弄清楚發生了什么,我將非常感謝。 顯然,我做錯了,這就是為什么PHP解釋器無法解析代碼的原因。 謝謝!

這是我的整個代碼(如果有幫助的話):

<html>
 <body>

   <?php
     require_once('TwitterAPIExchange.php');
     $settings = array(
         'oauth_access_token' => "N/A",
         'oauth_access_token_secret' => "N/A",
         'consumer_key' => "N/A",
         'consumer_secret' => "N/A"
     );

     $url = 'https://api.twitter.com/1.1/users/show.json';
     $getfield = '?screen_name=kobebryant';
     $requestMethod = 'GET';

     $twitter = new TwitterAPIExchange($settings);
     $json = $twitter->setGetfield($getfield)
            ->buildOauth($url, $requestMethod)
            ->performRequest();

    $result = json_decode($json);

    $url = $result->profile_image_url;
    <img src = "<?php echo ($url);?>"/> //This is the line giving me an error
     ?>
 </body>
 </html>

您關閉php標簽太晚了。 這就是為什么建議從html拆分php的原因。 請嘗試以下代碼:

<?php
    require_once('TwitterAPIExchange.php');
    $settings = array(
        'oauth_access_token' => 'N/A',
        'oauth_access_token_secret' => 'N/A',
        'consumer_key' => 'N/A',
        'consumer_secret' => 'N/A'
    );

    $url = 'https://api.twitter.com/1.1/users/show.json';
    $getfield = '?screen_name=kobebryant';
    $requestMethod = 'GET';

    $twitter = new TwitterAPIExchange($settings);
    $json = $twitter->setGetfield($getfield)
        ->buildOauth($url, $requestMethod)
        ->performRequest();

    $result = json_decode($json);

    $url = $result->profile_image_url;
?>
<html>
    <head></head>
    <body>
        <img src="<?php echo ($url);?>"/> //This is the line giving me an error       
    </body>
</html>

提示:如果您只是一個沒有變量的字符串,請使用'而不是"因為php試圖解析"之間的內容,這會浪費CPU資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM