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?
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 :\
?>
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);
}
};
Then to use
<?php
header("Content_Type: image/jpeg");
echo getUserImage("quagmire");
or
<img src="<?= getUrlImage("quagmire", true) ?>"/>