<?php
# This code was created by Good News Publishers (webmaster@gnpcb.org) in February 2003
# as a sample implementation of the ESV Web Service API (http://www.gnpcb.org/esv/share/services/).
# You are free to modify it however you wish.
############### Set the key to match your access key. Leaving it as "TEST" won't produce usable results.
$key "TEST";

$q = (isset($_REQUEST["q"])) ? stripslashes($_REQUEST["q"]) : "";
if (
strlen($q) > 0) {
    
$bgurl "http://www.biblegateway.com/bible?version=ESV&amp;"#you probably wouldn't hardcode the ESV into the link
    
list($xmldata$esvurl) = get_query_info($q);
    list(
$querytype$readable) = parse_query_info($xmldata$q);
    if (
$querytype) {
        
$bglink "$bgurl$querytype=" urlencode($readable);
        
#here you would probably go straight to the Bible Gateway:
        # header("Location: $bglink");
        
}
    else { 
#otherwise there was an error. We don't do anything with it, but you could do:
        # print htmlspecialchars($xmldata);
        
}
    }

function 
get_query_info($q) { #ask the ESV server to get information about the query
    
global $key;
    
$q urlencode($q);
    
$url "http://www.gnpcb.org/esv/share/get/?key=$key&action=getQueryInfo&q=$q";
    
$ch curl_init($url);  #we use curl; if you don't have it installed, you'll need to try something else--an include()?
    
curl_setopt($chCURLOPT_RETURNTRANSFER1); 
    
$xmldata curl_exec($ch);
    
curl_close ($ch);
    return array(
$xmldata$url);
    }

function 
parse_query_info($xmldata$q) { #get the information we need to make the string
    
if (strpos($xmldata"<error>")) { #will never be at position 0, so we don't need to check true/false
        
return array("""");
        }
    if (
strpos($xmldata"<query-type>passage")) { #if it's a passage
        
$bgtype "passage";
        
preg_match("/<readable>([^<]*?)</"$xmldata$matches); #get the format Bible Gateway understands
        
$readable $matches[1];
        }
    else { 
#otherwise it's a word search
        
$bgtype "search";
        
$readable $q;
        }
    return array(
$bgtype$readable);
    }
?>