Posts tagged php:

Sep posted 13 Sep 2009 and tagged

Amazon recently changed its Product API requirements and it was a little tricky to get the new signature working, so here’s an example of a product search in PHP.

function _encode_query($s) { $s = str_replace(',', urlencode(','), $s); $s = str_replace(':', urlencode(':'), $s); $s = str_replace(';', urlencode(';'), $s); return $s; } function _encode_signature($s) { $s = str_replace('+', urlencode('+'), $s); $s = str_replace('=', urlencode('='), $s); return $s; } function fetch($power_query, $access_key_id, $associate_tag) { $url_base = 'http://webservices.amazon.com/onca/xml?'; $url_params = array( 'Operation' => 'ItemSearch', 'Service' => 'AWSECommerceService', 'AWSAccessKeyId' => $access_key_id, 'Version' => '2009-01-06', 'Sort' => 'salesrank', 'ResponseGroup' => 'ItemAttributes,Images', 'SearchIndex' => 'Books', 'Power' => $power_query, 'Availability' => 'Available', 'Condition' => 'All', 'Timestamp' => gmdate('Y-m-d\TH:i:s\Z') ); $pairs = array(); foreach($url_params as $k => $v) { $pairs[] = $k.'='.$this->_encode_query($v); } sort($pairs); $canonical = implode('&', $pairs); $base = 'GET'.PHP_EOL.'webservices.amazon.com'.PHP_EOL.'/onca/xml'.PHP_EOL; $to_sign = $base.$canonical; $signature = base64_encode(hash_hmac('sha256', $to_sign, $associate_tag, true)); $signature = $this->_encode_signature($signature); $request = $url_base.$canonical.'&Signature='.$signature; $response = file_get_contents($request); $xml = simplexml_load_string($response); if (!is_object($xml)) { return null; } else { $products = array(); if (sizeof($xml->Items->Item) > 0) { foreach($xml->Items->Item as $item) { $products[] = $item; } } return $products; } }

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function _encode_query($s) {
  $s = str_replace(',', urlencode(','), $s);
  $s = str_replace(':', urlencode(':'), $s);
  $s = str_replace(';', urlencode(';'), $s);
  return $s;
}
 
function _encode_signature($s) {
  $s = str_replace('+', urlencode('+'), $s);
  $s = str_replace('=', urlencode('='), $s);
  return $s;
}
 
function fetch($power_query, $access_key_id, $associate_tag) {
  $url_base = 'http://webservices.amazon.com/onca/xml?';
  $url_params = array(
    'Operation' => 'ItemSearch',
    'Service' => 'AWSECommerceService',
    'AWSAccessKeyId' => $access_key_id,
    'Version' => '2009-01-06',
    'Sort' => 'salesrank',
    'ResponseGroup' => 'ItemAttributes,Images',
    'SearchIndex' => 'Books',
    'Power' => $power_query,
    'Availability' => 'Available',
    'Condition' => 'All',
    'Timestamp' => gmdate('Y-m-d\TH:i:s\Z')
  );
  $pairs = array();
  foreach($url_params as $k => $v) {
    $pairs[] = $k.'='.$this->_encode_query($v);
  }
  sort($pairs);
  $canonical = implode('&', $pairs);
  $base = 'GET'.PHP_EOL.'webservices.amazon.com'.PHP_EOL.'/onca/xml'.PHP_EOL;
  $to_sign = $base.$canonical;
  $signature = base64_encode(hash_hmac('sha256', $to_sign, $associate_tag, true));
  $signature = $this->_encode_signature($signature);
  $request = $url_base.$canonical.'&Signature='.$signature;
  $response = file_get_contents($request);
  $xml = simplexml_load_string($response);
  if (!is_object($xml)) {
    return null;
  } else {
    $products = array();
    if (sizeof($xml->Items->Item) > 0) {
      foreach($xml->Items->Item as $item) {
        $products[] = $item;
      }
    }
    return $products;
  }
}

Sep posted 13 Sep 2009 and tagged

This function generates a linear gradient between two hex values. Specify the number of hex values in the returned array with $color_steps. (Haven’t used this in a long time, but if I’m not more diligent about throwing snippets up here, I’ll lose track of them forever.)

function get_gradient($hex_from, $hex_to, $color_steps) { $from_rgb = array( 'r' => hexdec(substr($hex_from, 0, 2)), 'g' => hexdec(substr($hex_from, 2, 2)), 'b' => hexdec(substr($hex_from, 4, 2)) ); $to_rgb = array( 'r' => hexdec(substr($hex_to, 0, 2)), 'g' => hexdec(substr($hex_to, 2, 2)), 'b' => hexdec(substr($hex_to, 4, 2)) ); $step_rgb = array( 'r' => ($from_rgb['r'] - $to_rgb['r']) / ($color_steps - 1), 'g' => ($from_rgb['g'] - $to_rgb['g']) / ($color_steps - 1), 'b' => ($from_rgb['b'] - $to_rgb['b']) / ($color_steps - 1) ); $gradient_colors = array(); for($i = 0; $i < $color_steps; $i++) { $rgb = array( 'r' => floor($from_rgb['r'] - ($step_rgb['r'] * $i)), 'g' => floor($from_rgb['g'] - ($step_rgb['g'] * $i)), 'b' => floor($from_rgb['b'] - ($step_rgb['b'] * $i)) ); $hex_rgb = array( 'r' => sprintf('%02x', ($rgb['r'])), 'g' => sprintf('%02x', ($rgb['g'])), 'b' => sprintf('%02x', ($rgb['b'])) ); $gradient_colors[] = implode(null, $hex_rgb); } return $gradient_colors; }

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function get_gradient($hex_from, $hex_to, $color_steps) {
  $from_rgb = array(
    'r' => hexdec(substr($hex_from, 0, 2)),
    'g' => hexdec(substr($hex_from, 2, 2)),
    'b' => hexdec(substr($hex_from, 4, 2))
  );
  $to_rgb = array(
    'r' => hexdec(substr($hex_to, 0, 2)),
    'g' => hexdec(substr($hex_to, 2, 2)),
    'b' => hexdec(substr($hex_to, 4, 2))
  );
  $step_rgb = array(
    'r' => ($from_rgb['r'] - $to_rgb['r']) / ($color_steps - 1),
    'g' => ($from_rgb['g'] - $to_rgb['g']) / ($color_steps - 1),
    'b' => ($from_rgb['b'] - $to_rgb['b']) / ($color_steps - 1)
  );
  $gradient_colors = array();
  for($i = 0; $i < $color_steps; $i++) {
    $rgb = array(
      'r' => floor($from_rgb['r'] - ($step_rgb['r'] * $i)),
      'g' => floor($from_rgb['g'] - ($step_rgb['g'] * $i)),
      'b' => floor($from_rgb['b'] - ($step_rgb['b'] * $i))
    );
    $hex_rgb = array(
      'r' => sprintf('%02x', ($rgb['r'])),
      'g' => sprintf('%02x', ($rgb['g'])),
      'b' => sprintf('%02x', ($rgb['b']))
    );
    $gradient_colors[] = implode(null, $hex_rgb);
  }
  return $gradient_colors;
}

Example:

get_gradient('#0084b4', '#a9e0f4', 10); ( [0] => 0084b4 [1] => 128ebb [2] => 2598c2 [3] => 38a2c9 [4] => 4bacd0 [5] => 5db7d7 [6] => 70c1de [7] => 83cbe5 [8] => 96d5ec [9] => a9e0f4 )

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
14
get_gradient('#0084b4', '#a9e0f4', 10);
 
(
    [0] => 0084b4
    [1] => 128ebb
    [2] => 2598c2
    [3] => 38a2c9
    [4] => 4bacd0
    [5] => 5db7d7
    [6] => 70c1de
    [7] => 83cbe5
    [8] => 96d5ec
    [9] => a9e0f4
)