Skip to content

Instantly share code, notes, and snippets.

@Odalrick
Last active October 12, 2018 09:20
Show Gist options
  • Select an option

  • Save Odalrick/9eb338edb94e52369702549b37d30ba5 to your computer and use it in GitHub Desktop.

Select an option

Save Odalrick/9eb338edb94e52369702549b37d30ba5 to your computer and use it in GitHub Desktop.
test WM3 swecandy api
<?php
function mergeHeadersG($headers)
{
foreach ($headers as $key => $value)
yield $key . ': ' . $value;
}
function mergeHeaders($headers)
{
return implode("\r\n", iterator_to_array(mergeHeadersG($headers)));
}
function main()
{
$root = 'http://klippkungen.max-order.wm3-staging.se';
$token = '__kmz1MMzjV45z4gYTKQ_A';
$startHeaders = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'WM3 ' . base64_encode($token),
];
$p = function ($path) use ($root) {
return $root . $path;
};
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => mergeHeaders($startHeaders),
'method' => 'GET',
)
);
$context = stream_context_create($options);
$auth = function ($url) use ($context) {
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */
throw new RuntimeException('Something failed');
}
};
$result = $auth($p('/api/v1/shop/admin/countries'));
var_export($result);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment