Jump to content

JSON endpoint obect missing


meylo
Go to solution Solved by Ch0cu3r,

Recommended Posts

Can anyone help by shedding some light with a little problem I encounter using JSON endpoint requests. I'm not very good with PHP. When the endpoint is called and finds no data, I want to replace it with a local image source. In this case, I am calling brewery->logo_url. Most entries have endpoints, but some don't. When an image link is found at the endpoint, it throws off my layout. You can see the ones that look off. They align to the left. I want to replace with a logo default image each time this happens, with hopes to set the layout adjust back to normal.

 

Here's my working document http://graintoglass.com/fb/tap-menu-test.php

 

and here's the JSON. http://mcallen.taphunter.com/widgets/locationWidget?location=Grain-To-Glass&format=jsonv2short

 

Code Effort

 

<?php $url = 'http://mcallen.taphunter.com/widgets/locationWidget?location=Grain-To-Glass&format=jsonv2short&servingtype=bottlecan';
$curl_session = curl_init($url);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($curl_session, CURLOPT_TIMEOUT, 10);
$data = curl_exec($curl_session);
curl_close($curl_session);
$beer_list = json_decode($data);
echo '<p class="last-updated">Last Updated: ' . date("m/d/Y") . '</p>';
echo '<h1 class="page-title">Bottle List</h1>';
echo '<p class="page-intro">Not only do we serve a hefty list of brews on tap, we also offer an exclusive list of beers in bottle. Our bottle list is constantly changing so the occasional return visits to browse this list is the best way to stay up to date. Take a look!</p>';
foreach ($beer_list as $beer)
{
    echo '<article class="beer-entry">
    <div class="beer-image"><img src="'.$beer->brewery->logo_url.'" /></div>
    <div class="beer-info">
        <p class="brewery-name">'.$beer->brewery->name.' — '.$beer->brewery->origin.'</p>
        <h2 class="beer-name">'.$beer->beer->name.'</h2>
        <p class="beer-style">Beer Style: '.$beer->beer->style.'</p>
        <p class="beer-description">'.$beer->descriptions->short_description.'</p>
        <p class="beer-abv">ABV: '.$beer->beer->abv.'  /  IBU: '.$beer->beer->ibu.'</p>
        <p class="beer-ibu"></p>
    </div>

    <div class="clearfix"></div>
    </article>
    '; 
}
echo '<p class="page-bottom">If you would like to see more of our food and beer options, <a href="https://www.graintoglass.com" target="_blank">visit our website</a>.</p>';

?>
Edited by meylo
Link to comment
Share on other sites

  • Solution

You ned to check to see if $beer->brewery->logo_url is empty then display your default image. If its not then display the supplied brewery logo. Example code

$logo_image = empty($beer->brewery->logo_url) ? 'default-brewer-logo.png'   // use this default image if brewery->logo_url is empty
                                              : $beer->brewery->logo_url;   // use the brewery logo from json if it is not empty

...

echo '<article class="beer-entry">
    <div class="beer-image"><img src="'.$logo_image.'" /></div>
Link to comment
Share on other sites

Thank you for the help. Here's what I am getting with the added code you provided.

 

Test Page: http://graintoglass.com/fb/tap-menu-test.php

 

It seems that I get the default logo all the way through. I just need the default logo to show when the image endpoint is empty.

 

Most recent code effort

 

<?php $url = 'http://mcallen.taphunter.com/widgets/locationWidget?location=Grain-To-Glass&format=jsonv2short';
	$curl_session = curl_init($url);
	curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, 4);
	curl_setopt($curl_session, CURLOPT_TIMEOUT, 10);
	$data = curl_exec($curl_session);
	curl_close($curl_session);
	$beer_list = json_decode($data);
	$logo_image = empty($beer->brewery->logo_url) ? 'GrainToGlassExclusive.gif'   // use this default image if brewery->logo_url is empty
                                              : $beer->brewery->logo_url;   // use the brewery logo from json if it is not empty
	
	echo '<p class="last-updated">Last Updated: ' . date("m/d/Y") . '</p>';
	echo '<h1 class="page-title">Tap Menu</h1>';
	echo '<p class="page-intro">You have stumbled upon the treasure chest of delicious drinking. From dark porters and stouts to light pilsners and hefeweizens, we have a variety of beer styles to match your beer palette.  You might want to refresh your browser often, because we add new kegs daily. It is safe to say our tap list is always up to date.</p>';
	foreach ($beer_list as $beer)
	{
		echo '<article class="beer-entry">
		<div class="beer-image"><img src="'.$logo_image.'" />
		</div>
		<div class="beer-info">
			<p class="brewery-name">'.$beer->brewery->name.' — '.$beer->brewery->origin.'</p>
			<h2 class="beer-name">'.$beer->beer->name.'</h2>
			<p class="beer-style">Beer Style: '.$beer->beer->style.'</p>
			<p class="beer-description">'.$beer->descriptions->short_description.'</p>
			<p class="beer-abv">ABV: '.$beer->beer->abv.'  /  IBU: '.$beer->beer->ibu.'</p>
			<p class="beer-ibu"></p>
		</div>

		<div class="clearfix"></div>
		</article>
		'; 
	}
	echo '<p class="page-bottom">If you would like to see more of our food and beer options, <a href="https://www.graintoglass.com" target="_blank">visit our website</a>.</p>';
	
?>
Edited by meylo
Link to comment
Share on other sites

I don't know how you're not getting an error, considering there is no $beer variable where you copied and pasted the provided code.

 

*Think* about what you're doing here. You want to check the logo url *for each* beer in the list, right?

 

Oh, and you probably want to fix your gif so that EXCLUSIVE is spelled properly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.