Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How does this script work?

I have no knowledge of php/ unsure if it's the right place to ask.

6:51 pm on Aug 29, 2012 (gmt 0)

New User

5+ Year Member

joined:Aug 29, 2012
votes: 0

Hi there,

Before I start, you should know I am not a programmer. I have no clue what I'm doing at this point and basically am asking that you are patient. Thank you.

To give you a context, my goal is to submit data via a form to generate a list of results. My AM sent me a sample script that I assume will automatically work with their API.

I'll add the code below, and I also received a html template that I assume will be used to format the results. My problem is I have no idea what to do with the script.

How is the form suppose to send the data to the script and how is the script suppose to generate the results? Am I suppose to put this between HTML tags?

// 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
// string.
$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__";
$request->affiliateKey ="__INSERT_YOUR_AFFILIATE_KEY_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/",
// init the curl handler
$ch = curl_init();
// set the options
curl_setopt_array($ch, $curl_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);
echo urldecode($resultObj->main->renderedListing);

I know this is probably a lot to ask but if someone could point me in the right direction it would help a lot.



[edited by: tedster at 3:05 pm (utc) on Aug 30, 2012]
[edit reason] switch to example.com [/edit]

9:19 pm on Jan 20, 2013 (gmt 0)


WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
votes: 2

Hello liwii and welcome to WebmasterWorld.

The script is using the cURL API which allows you to, among many other things, make POST requests from your server to another server and then read the results from that POST request. You are expected to set some parameters and then invoke this PHP script to POST data to your AM.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members