2013-07-19 43 views
0

我尝试这样做: 负荷http://example.com/nickname/picture(昵称被加载在您访问的网址一个变种,that's工作) 解码为JSON 的网站是PHP加载内容(图片)(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"]); 

我知道我didn't处理在这个脚本错误之类的东西,但我希望它与真实的图像工作第一之前。

所以平均的问题:如何显示和解码json网站的图像?

回答

0

您真的需要curl吗?
您也可以替换file_get_contentscurlGet在我的代码

<?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 :\ 
?> 
+0

你真的需要卷曲 - 嗯,看起来像一个没有。谢谢!你真的帮了我 – Mazey

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) ?>"/>