0

我正在尝试这样做:加载http://example.com/nickname/picture(昵称在您访问的 url 中作为 var 加载,这是有效的)解码为 json 站点是这样的

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

加载我尝试过的图片:

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"]);

我知道我没有处理这个脚本中的错误和东西,但我希望它先处理真实图像。

所以最重要的问题是:如何从解码的 json 站点显示和图像?

4

2 回答 2

0

你真的需要curl吗?
您也可以在我的代码中file_get_contents替换为curlGet

<?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 :\
?>
于 2013-07-19T14:44:55.513 回答
0
function getUserImage($nick, $imgUrl = false) {
        $jsonString = file_get_contents("http://example.com/".$nickname."/picture");
        $json = json_decode($jsonString);
        if ($imgUrl){
            return $json->picture;
        } else {
            return file_get_contents($json->picture);
        }
};

然后使用

<?php
header("Content_Type: image/jpeg");
echo getUserImage("quagmire");

或者

<img src="<?= getUrlImage("quagmire", true) ?>"/>
于 2013-07-19T14:46:35.907 回答