Skip to content

Instantly share code, notes, and snippets.

@ajslaghu
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajslaghu/de260773a74f12c77d02 to your computer and use it in GitHub Desktop.
Save ajslaghu/de260773a74f12c77d02 to your computer and use it in GitHub Desktop.
Bol.com Open API example script for Dutch Open Hackathon
<?php
//Requires: extension=php_openssl.dll in php.ini
// Based on bol.com REST API version 4.0
// No rights may be derived from this codesample
// Any questions? Check out the API documentation at http://developers.bol.com
// set apikey
$apiKey = '___your_apikey___';
$server = 'openapi.bol.com';
$port = '443';
// Set keyword
$keyword = 'bureaustoel';
function doRequest($method, $server, $port, $url, $parameters, $content, $sessionId) {
$contentType = 'application/json';
$headers = $method . " " . $url . $parameters . " HTTP/1.0\r\nContent-type: " . $contentType . "\r\n";
$headers .= "Host: " . $server . "\r\n";
$headers .= "Connection: close\r\n";
$headers .= "\r\n";
// Connect using fsockopen (you could also try CURL)
$socket = fsockopen('ssl://' . $server, $port, $errno, $errstr, 30);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
}
fputs($socket, $headers);
fputs($socket, $content);
$ret = "";
while (!feof($socket)) {
$readLine = fgets($socket);
$ret .= $readLine;
}
fclose($socket);
return $ret;
}
// Build the request to the API
$output = doRequest('GET', $server, $port, '/catalog/v4/search', '?q=' . urlencode($keyword) . '&apikey=' . $apiKey . '&offset=0&nrProducts=8includeattributes=true&dataoutput=categories,refinements,products&ids=0', '', null);
// Check for the right http status in the API respons
if (substr_count($output, "200 OK") > 0) {
// Strip unneeded stuff from the json respons
list($header, $body) = explode("\r\n\r\n", $output, 2);
// decode the json sting
$phpobject = json_decode($body);
// Get the products
$price_arr = array();
foreach ($phpobject->products as $item) {
// The first offer is always a bol.com offer (unless this product is only sold by 2ndhand or plaza partners
$price = doubleval($item->offerData->offers[0]->price);
$price_arr[] = $price;
}
$price_sum = array_sum($price_arr);
$price_count = count($price_arr);
$avg_price = ( $price_sum / count($price_arr) );
echo "Average price is $avg_price\n";
die;
// End of statuscode 200
}
die('no 200 OK');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment