douya8978 2013-07-19 14:38
浏览 24
已采纳

PHP从另一个URL(json)加载URL的内容(图片)

im trying to do this: load http://example.com/nickname/picture (nickname is loaded as a var in the url you visit, that´s working) decode to json the site is this

{
"picture": "https://example.com/hf8329yrh8oq.jpg"
}

load the picture what i tried:

function curlGet($url)
   {
   $crl = curl_init();
   $timeout = 5;
   curl_setopt ($crl, CURLOPT_URL,$url);
   curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
   $status = curl_exec($crl);
   curl_close($crl);
   return $status;
   }
$profPicCurl = curlGet('https://example.com/'.urlencode($_GET['nickname']).'/picture')
$profPic = json_decode($profPicCurl,true);
echo file_get_contents($profPic.["picture"]);

i know I didn´t handle errors and stuff in this script, but i want it to work with a real image first before.

so the mean question: how to display and image from a decoded json site?

  • 写回答

2条回答 默认 最新

  • 普通网友 2013-07-19 14:44
    关注

    Do you really need curl?
    You can also replace file_get_contents with curlGet in my code

    <?php
      $profPic  = json_decode( file_get_contents( 'https://example.com/'.urlencode( $_GET['nickname'] ).'/picture' ) );
      if ( $profPic ) { // is a valid json object
        if ( isset( $profPic->picture ) ) { // profile picture exists
          $profPic  = $profPic->picture;
          $extension  = strtolower( pathinfo( $profPic, PATHINFO_EXTENSION ) ); // get the image extension
          $content  = file_get_contents( $profPic ); // get content of image
          if ( $content ) {
            header( "Content-type: image/".$extension ); // set mime type
            echo $content; // output the content
            exit();
          }
        }
      }
      echo "File not found"; // there is some errors :\
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理