Echo Doens't Work in My Server

Please help. Doing a project that can get Gold Price from the Web.

Tried this function and it works on my LOCALHOST but not in my free server.


<!DOCTYPE html>


<?php


function get_gold(){
	$url='http://money.cnn.com/data/commodities/';
	//$url='http://vikingspawnshop.net16.net/getgold.php';
	$data = file_get_contents($url);
	//preg_match("/[0-9]*\,[0-9]*\..[0-9]*/",$data,$converted);
	preg_match("/[0-9]*\,[0-9]*\..[0-9]*/",$data,$converted);
	echo $converted[0];
}


?>
<html>
<head>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<link href="../css/bootstrap.min.css" rel="stylesheet" />
	<link href="../css/mystyle.css" rel="stylesheet" />
	<script src="../css/jquery.js"></script>
	<script src="../css/jquerycycle.js"></script>
	<script src="../js/bootstrap.min.js"></script>
	<title>Home - Viking's Pawnshop</title>
</head>
<body>
<div class="container">

	<nav class="navbar navbar-default navbar-static-top">
		<div class="container-fluid">

			<div class="navbar-header">
				<button class="navbar-toggle" data-toggle="collapse" data-target=".navHeaderCollapse">
				  <span class="icon-bar"></span>
				  <span class="icon-bar"></span>
				  <span class="icon-bar"></span>
				</button>

				<img alt="Brand" src="logo.jpg" height="40px" width="40px" style="float: left" class="logo">
				<a class="navbar-brand" href="#">Viking's Pawnshop</a>
			</div>
		  <div class="collapse navbar-collapse navHeaderCollapse">
			<ul class="nav navbar-nav navbar-right">
				<li><a href="index.php">Home</a></li>
				<li><a href="shop.php">Items For-Sale</a></li>
				<li><a href="aboutus.php">About Us</a></li>
				<li><a href="contactus.php">Contact Us</a></li>
			</ul>
		  </div>

		</div>
	</nav>
	<div class="row">
		<h1 style="color: blue">Gold Price: <?php echo get_gold()." USD"; ?></h1>
	</div>
	
	<div class="row"><!--Start of Footer Row-->
		<div class="col-md-12" id="footer-container"><!--Start of Block 1-->
			<div class="footer"><font class="footer-text">
				<div class="col-md-12 col-sm-12 col-xs-12"><!--Start of Copyright-->
					<p class="text-center">Copyright © 2016 Viking Pawnshop Inc. </p>
					<p class="text-center">Developed by: <a href="http://marlondiaz.ml">MDSSL</a></p></font>
				</div><!--Start of Copyright-->
			</div>
			
		</div><!--End of Block 1-->
		
	</div><!--End of Footer Row-->

</div><!--End of Main Container-->
</body>
</html>
<script>
</script>

whats the error message? increase error_reporting

does file_get_contents work? We have an api that stopped working the other day as the security on the site we were picking the json file up from had changed and was stopping file_get_contents. Just try a simple test like

<?php $test = file_get_contents('http://www.example.com'); echo $test;?>

Think that should work although just typed that now so might have missed something (others might have to correct). But hopefully that should show you if file_get_contents is working or not.

Hi @chorn, there is no error
just that the value isn’t displaying.

you can check it out using http://vikingspawnshop.net16.net/goldprice.php

In my local host, the $value shows up.

http://vikingspawnshop.net16.net/goldprice.php

The $value isn’t showing up in the uploaded version @Noppy.
Tried editing the codes but still, it shows in my localhost but the uploaded version. :frowning:

You have an echo inside the function and you echo the function when you call it. I’m not sure which is right, but I can;t see you want both!

Here’s the updated code. Still not working.

<?php $url='http://money.cnn.com/data/commodities/'; //$url='http://vikingspawnshop.net16.net/getgold.php'; $data = file_get_contents($url); //preg_match("/[0-9]*\,[0-9]*\..[0-9]*/",$data,$converted); preg_match("/[0-9]*\,[0-9]*\..[0-9]*/",$data,$converted); $value=$converted[0]; ?> Home - Viking's Pawnshop
<nav class="navbar navbar-default navbar-static-top">
	<div class="container-fluid">

		<div class="navbar-header">
			<button class="navbar-toggle" data-toggle="collapse" data-target=".navHeaderCollapse">
			  <span class="icon-bar"></span>
			  <span class="icon-bar"></span>
			  <span class="icon-bar"></span>
			</button>

			<img alt="Brand" src="logo.jpg" height="40px" width="40px" style="float: left" class="logo">
			<a class="navbar-brand" href="#">Viking's Pawnshop</a>
		</div>
	  <div class="collapse navbar-collapse navHeaderCollapse">
		<ul class="nav navbar-nav navbar-right">
			<li><a href="index.php">Home</a></li>
			<li><a href="shop.php">Items For-Sale</a></li>
			<li><a href="aboutus.php">About Us</a></li>
			<li><a href="contactus.php">Contact Us</a></li>
		</ul>
	  </div>

	</div>
</nav>
<div class="row">
	<h1 style="color: blue">Gold Price: <?php echo $value." ".'USD'; ?></h1>
</div>

<div class="row"><!--Start of Footer Row-->
	<div class="col-md-12" id="footer-container"><!--Start of Block 1-->
		<div class="footer"><font class="footer-text">
			<div class="col-md-12 col-sm-12 col-xs-12"><!--Start of Copyright-->
				<p class="text-center">Copyright © 2016 Viking Pawnshop Inc. </p>
				<p class="text-center">Developed by: <a href="http://marlondiaz.ml">MDSSL</a></p></font>
			</div><!--Start of Copyright-->
		</div>
		
	</div><!--End of Block 1-->
	
</div><!--End of Footer Row-->

The last statement in your function wants to be

return $converted[0];

I think. If that doesn’t work I would check the versions of PHP on your localhost and on your host.

1 Like

Guys, found the problem. My free hosting wont let me post it.
By the way, thanks for all your help.

Your free hosting won’t let you crawl cnn.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.