user4923: This is a pretty classic programming question, but I think it is a very common one for the Bitcoin community, and would be helpful to have the code publicly available.
My preference is for a php script.

Bitpay publishes it's exchange rates here: https://bitpay.com/api/rates
If an item is $10 USD, how would I do the following?
1) extract the BTC/USD exchange rate from the array at this URL
2) Define the price, then divide by the exchange rate
3) Display the BTC price in an HTML page?  

user30234: Here's a bash command that gets the spot price from the Coinbase API (TM). It pipes the response to jq, which parses it and outputs the value(s) specified in the filter passed to it. -r strips the quotes from its output, which is passed to sed. sed processes the first 4 digits of this output, inserting a thousands comma and dollar sign. Note that it only works for 4-digit numbers (so will need to be modified shortly, replacing the '1' with a '2' ;-) The output redirection 2>/dev/null hides the curl curses interface:
curl --insecure https://coinbase.com/api/v1/prices/spot_rate 2>/dev/null | jq -r '.["amount"]' | sed 's/\([[:digit:]]\{1\}\)\([[:digit:]]\{3\}\)/\$\1,\2/g'


user4923: An older, maybe somewhat wiser me, answering my own question:
Here's some PHP to get the exchange rate. 
<?php

$page = file_get_contents('https://bitpay.com/api/rates');
$my_array = json_decode($page, true);
$exchange_rate = $my_array[0]["rate"];      

?>

I eventually found it easiest to do in javascript, since you're going to want to display the price as well.
Anywhere you have an element with `class="price", this will add the mBTC equivalent next to it. Tweak to your liking. 
$(document).ready(function(){
  $.getJSON('https://bitpay.com/api/rates', function(data){
    var rate = data[0].rate;

    $(".price").each(function(i){
      var tex = $(this).text();
      var usdPrice = Number(tex.replace(/[^0-9.-]+/g,"")); 
      var mbtcPrice = 1000*usdPrice/rate;
      $(this).append("(&nbsp;~" + String(mbtcPrice.toFixed(0))+ "&nbsp;mBTC)");
    }); 
  });
});


user5731: Here is an example that retrieves the current current values from the json stream at mtgox (current, low, average, last, and now).   It is written in python, but should be very easy to port it over to the language of your choice. 
#https://github.com/spudstud/alfred-bitcoin-workflow

     #json is the standard python json parser
     #urllib2 is a webpage fetching library
import json     
import urllib2  

#The url that provides the raw json stream
url="http://data.mtgox.com/api/2/BTCUSD/money/ticker"

#Open the url and turn it into an string containing the json
jsonURL=urllib2.urlopen(url)

#Convert the json text into a json object
jsonObject=json.load(jsonURL)

#Parse the desired values out of the json object
currentValue    = jsonObject['data']['high']['display']
lowValue        = jsonObject['data']['low']['display']
avgValue        = jsonObject['data']['avg']['display']
lastValue       = jsonObject['data']['last']['display']
nowValue        = jsonObject['data']['now']

#Sometimes your json is not a string,you will need to use json.load, not json.loads:

print jsonObject
print currentValue
print lowValue
print avgValue
print lastValue
print nowValue


user7043: Since Mt. Gox is down and the market is split, I would suggest using blockchain.info's API. I believe they combine different current market prices to make one average price. This is much more reliable than trusting one market only (see what happened to Mt. Gox). Also blockchain.info is a generally well trusted site.
To load the information in PHP use the file_get_contents() and json_decode() functions. Most of these functions are available in other high-end programming languages.

user6108: May be useful psp(python) example:
<%psp
import urllib2;     
import simplejson as json;     
j = json.loads(urllib2.urlopen("https://data.mtgox.com/api/2/BTCUSD/money/ticker").read());     
%>

<%=str(j["data"]["last"]["value"]) %>

Or one command line:
# /usr/bin/python -c 'import urllib2; import simplejson as json; j = json.loads(urllib2.urlopen("https://data.mtgox.com/api/2/BTCUSD/money/ticker").read()); print str(j["data"]["last"]["value"]);'


user-1: Save the code to a PHP file in the web directory: 
<?php  
  $url = "https://bitpay.com/api/rates";

  $json = file_get_contents($url);
  $data = json_decode($json, TRUE);

  $rate = $data[2]["rate"];   //$data[1] is outdated now, they have updated their json order. This new number 2 now fetches USD price. 
  $usd_price = 10;     # Let cost of elephant be 10$
  $bitcoin_price = round( $usd_price / $rate , 8 );
?>

<ul>
   <li>Price: <?=$usd_price ?> $ / <?=$bitcoin_price ?> BTC
</ul>


4923: <p>This is a pretty classic programming question, but I think it is a very common one for the Bitcoin community, and would be helpful to have the code publicly available.</p>

<p>My preference is for a php script.</p>

<hr>

<p>Bitpay publishes it's exchange rates here: <a href="https://bitpay.com/api/rates" rel="noreferrer">https://bitpay.com/api/rates</a></p>

<p>If an item is $10 USD, how would I do the following?</p>

<p>1) extract the BTC/USD exchange rate from the array at this URL<br>
2) Define the price, then divide by the exchange rate<br>
3) Display the BTC price in an HTML page?  </p>
 30234: <p>Here's a <code>bash</code> command that gets the spot price from the Coinbase API (TM). It pipes the response to <code>jq</code>, which parses it and outputs the value(s) specified in the filter passed to it. <code>-r</code> strips the quotes from its output, which is passed to <code>sed</code>. <code>sed</code> processes the first 4 digits of this output, inserting a thousands comma and dollar sign. Note that it only works for 4-digit numbers (so will need to be modified shortly, replacing the '1' with a '2' ;-) The output redirection <code>2&gt;/dev/null</code> hides the <code>curl</code> <code>curses</code> interface:</p>

<pre><code>curl --insecure https://coinbase.com/api/v1/prices/spot_rate 2&gt;/dev/null | jq -r '.["amount"]' | sed 's/\([[:digit:]]\{1\}\)\([[:digit:]]\{3\}\)/\$\1,\2/g'
</code></pre>
 4923: <p>An older, maybe somewhat wiser me, answering my own question:</p>

<p>Here's some PHP to get the exchange rate. </p>

<pre><code>&lt;?php

$page = file_get_contents('https://bitpay.com/api/rates');
$my_array = json_decode($page, true);
$exchange_rate = $my_array[0]["rate"];      

?&gt;
</code></pre>

<p>I eventually found it easiest to do in javascript, since you're going to want to display the price as well.</p>

<p>Anywhere you have an element with `class="price", this will add the mBTC equivalent next to it. Tweak to your liking. </p>

<pre><code>$(document).ready(function(){
  $.getJSON('https://bitpay.com/api/rates', function(data){
    var rate = data[0].rate;

    $(".price").each(function(i){
      var tex = $(this).text();
      var usdPrice = Number(tex.replace(/[^0-9.-]+/g,"")); 
      var mbtcPrice = 1000*usdPrice/rate;
      $(this).append("(&amp;nbsp;~" + String(mbtcPrice.toFixed(0))+ "&amp;nbsp;mBTC)");
    }); 
  });
});
</code></pre>
 5731: <p>Here is an example that retrieves the current current values from the json stream at <a href="http://data.mtgox.com/api/2/BTCUSD/money/ticker" rel="nofollow">mtgox</a> (current, low, average, last, and now).   It is written in python, but should be very easy to port it over to the language of your choice. </p>

<pre><code>#https://github.com/spudstud/alfred-bitcoin-workflow

     #json is the standard python json parser
     #urllib2 is a webpage fetching library
import json     
import urllib2  

#The url that provides the raw json stream
url="http://data.mtgox.com/api/2/BTCUSD/money/ticker"

#Open the url and turn it into an string containing the json
jsonURL=urllib2.urlopen(url)

#Convert the json text into a json object
jsonObject=json.load(jsonURL)

#Parse the desired values out of the json object
currentValue    = jsonObject['data']['high']['display']
lowValue        = jsonObject['data']['low']['display']
avgValue        = jsonObject['data']['avg']['display']
lastValue       = jsonObject['data']['last']['display']
nowValue        = jsonObject['data']['now']

#Sometimes your json is not a string,you will need to use json.load, not json.loads:

print jsonObject
print currentValue
print lowValue
print avgValue
print lastValue
print nowValue
</code></pre>
 7043: <p>Since Mt. Gox is down and the market is split, I would suggest using <a href="https://blockchain.info/api/exchange_rates_api" rel="nofollow">blockchain.info's API</a>. I believe they combine different current market prices to make one average price. This is much more reliable than trusting one market only (see what happened to Mt. Gox). Also <a href="https://blockchain.info" rel="nofollow">blockchain.info</a> is a generally well trusted site.</p>

<p>To load the information in PHP use the <code>file_get_contents()</code> and <code>json_decode()</code> functions. Most of these functions are available in other high-end programming languages.</p>
 6108: <p>May be useful psp(python) example:</p>

<pre><code>&lt;%psp
import urllib2;     
import simplejson as json;     
j = json.loads(urllib2.urlopen("https://data.mtgox.com/api/2/BTCUSD/money/ticker").read());     
%&gt;

&lt;%=str(j["data"]["last"]["value"]) %&gt;
</code></pre>

<p>Or one command line:</p>

<pre><code># /usr/bin/python -c 'import urllib2; import simplejson as json; j = json.loads(urllib2.urlopen("https://data.mtgox.com/api/2/BTCUSD/money/ticker").read()); print str(j["data"]["last"]["value"]);'
</code></pre>
 -1: <p>Save the code to a PHP file in the web directory: </p>

<pre><code>&lt;?php  
  $url = "https://bitpay.com/api/rates";

  $json = file_get_contents($url);
  $data = json_decode($json, TRUE);

  $rate = $data[2]["rate"];   //$data[1] is outdated now, they have updated their json order. This new number 2 now fetches USD price. 
  $usd_price = 10;     # Let cost of elephant be 10$
  $bitcoin_price = round( $usd_price / $rate , 8 );
?&gt;

&lt;ul&gt;
   &lt;li&gt;Price: &lt;?=$usd_price ?&gt; $ / &lt;?=$bitcoin_price ?&gt; BTC
&lt;/ul&gt;
</code></pre>
