| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides access to the Zanox Web Services API.
|
| 7 |
*/
|
| 8 |
|
| 9 |
function zws_request(Array $params) {
|
| 10 |
if (false === zws_required_settings()) {
|
| 11 |
return zws_required_settings_error();
|
| 12 |
}
|
| 13 |
$search = drupal_urlencode('madrid');
|
| 14 |
|
| 15 |
$zws_server = 'http://api.zanox.com';
|
| 16 |
$zws_request_method = 'GET';
|
| 17 |
|
| 18 |
// module settings
|
| 19 |
$zws_applicaton_id = utf8_encode('ZXWS ' . variable_get('zws_application_id', ''));
|
| 20 |
$zws_shared_key = variable_get('$zws_shared_key', '');
|
| 21 |
|
| 22 |
$zws_get_param_date = gmdate('D, d M Y H:i:s') .' GMT';
|
| 23 |
|
| 24 |
# adspaces
|
| 25 |
# $zws_resource_url = '/adspaces';
|
| 26 |
# $zws_resource_url = '/products/adspace/' . $zws_adspace_id . '?q=' . $search;
|
| 27 |
$zws_resource_url = '/products?q=' . $search;
|
| 28 |
$zws_string2sign = utf8_encode($zws_request_method . $zws_resource_url . $zws_get_param_date);
|
| 29 |
$zws_signature = base64_encode(hash_hmac( 'SHA1' , $zws_string2sign , $zws_shared_key, true));
|
| 30 |
$zws_applicaton_id_security = $zws_applicaton_id . ':' . $zws_signature;
|
| 31 |
$zws_request_url = $zws_server . '/json' . $zws_resource_url;
|
| 32 |
|
| 33 |
$headers = array();
|
| 34 |
$headers['Date'] = $zws_get_param_date;
|
| 35 |
$headers['Authorization'] = $zws_applicaton_id_security;
|
| 36 |
|
| 37 |
$response = drupal_http_request($zws_request_url, $headers, $zws_request_method);
|
| 38 |
var_dump($response);
|
| 39 |
if (200 == $response->code) {
|
| 40 |
$result = json_decode($response->data);
|
| 41 |
foreach ($result->productsResult->productItem as $item) {
|
| 42 |
var_dump($result);
|
| 43 |
#var_dump($item->url->adspace->{'$'});
|
| 44 |
}
|
| 45 |
}
|
| 46 |
else {
|
| 47 |
print $response->error;
|
| 48 |
}
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Implementation of hook_menu().
|
| 53 |
*/
|
| 54 |
function zws_menu() {
|
| 55 |
$items = array();
|
| 56 |
$items['zws/test'] = array(
|
| 57 |
'title' => 'Zanox Web Service Request Test',
|
| 58 |
'description' => 'Test requests to Zanox Web Service.',
|
| 59 |
'page callback' => 'zws_request',
|
| 60 |
'access arguments' => array('administer site configuration'),
|
| 61 |
'type' => MENU_NORMAL_ITEM
|
| 62 |
);
|
| 63 |
$items['admin/settings/zws'] = array(
|
| 64 |
'title' => 'Zanox API Settings',
|
| 65 |
'description' => 'Zanox Web Services API settings.',
|
| 66 |
'page callback' => 'drupal_get_form',
|
| 67 |
'page arguments' => array('zws_admin_settings'),
|
| 68 |
'access arguments' => array('administer site configuration'),
|
| 69 |
'type' => MENU_NORMAL_ITEM,
|
| 70 |
'file' => 'zws.admin.inc'
|
| 71 |
);
|
| 72 |
return $items;
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Tests whether required settings to access the zanox
|
| 77 |
* Web Services API exist
|
| 78 |
*
|
| 79 |
* @return Boolean
|
| 80 |
*/
|
| 81 |
function zws_required_settings() {
|
| 82 |
return variable_get('zws_application_id', '') && variable_get('zws_shared_key', '');
|
| 83 |
}
|
| 84 |
|
| 85 |
/**
|
| 86 |
* Returns an error message indicating missing required module settings
|
| 87 |
*
|
| 88 |
* @return String error message
|
| 89 |
*/
|
| 90 |
function zws_required_settings_error() {
|
| 91 |
$msg = 'Required settings to access Zanox Web Services missing. ';
|
| 92 |
$msg .= 'Go to the !url and add the missing settings.';
|
| 93 |
return t($msg, array('!url' => l('settings page', 'admin/settings/zws')));
|
| 94 |
}
|