Welcome to WebmasterWorld Guest from 22.214.171.124
// Sample php script that makes a call to the Smart Listings Rendering Server.
// It assumes that three parameters are passed in: $aoi, $doi and $template.
// $aoi/$doi are the comma separated area of interest and degree of interest
// codes from the spreadsheet we gave you. $template is the name of the
// template file that you create (you can use the sample from the api docs
// if you like, to get you started), placed in a directory called 'templates'
// that sits at the same level as the directory that holds the php script.
// Also please note that there are three things you need to replace in this
// script, one is your webSiteName, another is your affiliate key and the
// other your widgetInstanceKey.
// change to current directory so we can use relative paths
// read in the template. we need to do this because we have to url encode
// it before we can send it to sl.qmp.example.com, but if we leave it
// as an html file on disk it's easier to edit. make as many templates as you
// want/need and just set $template to the name (without .html) before including
// this file.
$slTemplate = file_get_contents("../templates/" . $template . ".html");
// now we need to create a proper request object that sl.qmp.example.com will
// understand. hand-creating json is a pain, so we'll use stdClass objects to build
// the request, then json_encode to convert the objects into a properly formed JSON
$request = new stdClass();
// this example code only handles a single widget (school listing) on the page ... in order to pass
// multiple SL widgets into the rendering server you'll have to add multiple request
// objects into a single POST. see the documentation for how to do this.
$request->id = "main";
// change the below to the webSiteName issued to you by example
$request->webSiteName = "__INSERT_YOUR_WEBSITE_NAME_HERE__";
// this is probably still right for you
$request->webPageUri = "/";
// this value is given to you by example
$request->widgetInstanceKey = "__INSERT_YOUR_WIDGETINSTANCEKEY_HERE__";
// for this example, show both campus and online schools
$request->campusType = "BOTH";
// $aoi/$doi are passed into this file by the file that you included in your page.
$request->areaOfInterest = $aoi;
$request->degreeOfInterest = $doi;
// encode the template - this is required
$request->template = rawurlencode($slTemplate);
// put the request into the 'widgets' wrapper - see the documentation
$postData = new stdClass();
$postData->widgets = array();
$postData->widgets = $request;
$postJson = json_encode($postData);
// we'll use cURL to do the http POST to the rendering server (sl.qmp.example.com)
// you probably don't want to change these values. pay particular attention to the low
// connect and transfer timeouts ... keep them low and retry for best results (retry logic is below)
$curl_options = array(
CURLOPT_URL => "http://sl.qmp.example.com/",
CURLOPT_PORT => 80,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postJson,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 1,
CURLOPT_TIMEOUT => 5
// init the curl handler
$ch = curl_init();
// set the options
// setup our retry logic ... this example retries two times if any cURL error or http code other than 200
// is returned
$tries = 0;
$err = -1;
$httpCode = -1;
while ($tries++ <= 2 && ($err !== 0 || $httpCode !== 200))
$result = curl_exec($ch);
$info = curl_getinfo($ch);
$httpCode = $info['http_code'];
$err = curl_errno($ch);
// check for successful result
if ($err !== 0 || $httpCode !== 200)
// Houston, we have a problem.
// you might want to redirect here to an error page on your site?
// you might want to log this on your server?
// you probably don't want to do what we're going to do here, but this will suffice as an example
echo "No schools found or there was a problem, please try again later. (error: $err, http code: $httpCode)";
// success! ... echo the decoded results to the page
$resultObj = json_decode($result);
[edited by: tedster at 3:05 pm (utc) on Aug 30, 2012]
[edit reason] switch to example.com [/edit]