view xml/amazonBookSearch.php @ 30:b1bbf44c701b

standarise semaphore use
author Robert Boland <robert@markup.co.uk>
date Fri, 04 Jan 2019 08:50:23 -0500
parents d606320ec331
children dbc006408d2b
line wrap: on
line source

<?php

$Adefault=array(
  'language'           =>'en',           //what language to render the page in
  'locale'             =>'us',           //which server's products? available: ca,de,fr,jp,uk,us
  'page'               =>1,              //first page to show (we are counting from 1 not 0)
  'operation'          =>'ItemSearch',   //what to do?	//ItemSearch
  'searchindex'        =>'Books',        //what product category for search?
  'searchparameter'    =>'Keywords',       //what kind of search?
  'searchparameterdata'=>'Machiavelli',  //what to search for?
  //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true'
  'show_url'           =>false,          //debug: show XML request url to be send to Amazon?
);

$Aassociates_id=array(
  'uk' => 'bookwhack-21',
  'us' => 'your02b-20',
  'ca' => 'book009-20',
  'de' => 'book04c-21',
  'fr' => 'book07f-21',
);

$Aserver=array(
  'ca' => array(
    'ext' => 'ca'                      ,  //Canadian normal server
    'nor' => 'http://www.amazon.ca'    ,  //Canadian normal server
    'xml' => 'http://xml.amazon.com'   ,  //Canadian xml server
  ),
  'de' => array(
    'ext' => 'de'                      ,  //German normal server
    'nor' => 'http://www.amazon.de'    ,  //German normal server
    'xml' => 'http://xml-eu.amazon.com',  //German xml server
  ),
  'fr' => array(
    'ext' => 'fr'                      ,  //French normal server
    'nor' => 'http://www.amazon.fr'    ,  //French normal server
    'xml' => 'http://xml-eu.amazon.com',  //French xml server
  ),
  'jp' => array(
    'ext' => 'jp'                      ,  //Japanese normal server, not co.jp!
    'nor' => 'http://www.amazon.co.jp' ,  //Japanese normal server
    'xml' => 'http://xml.amazon.com'   ,  //Japanese xml server
  ),
  'uk' => array(
    'ext' => 'co.uk'                   ,  //UK normal server
    'nor' => 'http://www.amazon.co.uk' ,  //UK normal server
    'xml' => 'http://xml-eu.amazon.com',  //UK xml server
  ),
  'us' => array(
    'ext' => 'com'                     ,  //USA normal server
    'nor' => 'http://www.amazon.com'   ,  //USA normal server
    'xml' => 'http://xml.amazon.com'   ,  //USA xml server
  ),
);

include "aws_signed_request.php";  
$public_key ="AKIAIHTNWC7L6LOUY4LQ";
$private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde";

//for all parameters see if the user has overruled it or use the default
foreach ($Adefault as $i=>$d) {
  $$i=isset($_GET[$i])?$_GET[$i]:$d;
}
//this is the data that is used to form the request for AWS
//this is the part that is search specific

  $parameters=array(
    'Operation'       =>$operation              ,
    //'Keywords'        =>urlencode($search)      ,
    'SearchIndex'     =>$searchindex            ,  //Books for example.
    "$searchparameter"=>stripslashes($searchparameterdata),
    'ItemPage'        =>$page                   ,  //which page?
    'AssociateTag'    =>$Aassociates_id[$locale],
	'MerchantId'	  =>'All',
	'Availability'	  =>'Available',
	'Condition'		  =>'All',
    'ResponseGroup'   =>'Images,ItemAttributes'                ,  //Small, Medium, Large or SellerListing
  );

$ext=$Aserver[$locale]['ext'];  
$file_data=$ext;
ksort($parameters);
foreach ($parameters as $i=>$d) {
  $file_data.='&'.$i.'='.$d;
}

$url=aws_signed_request($ext,$parameters,$public_key,$private_key);  
//echo $url;

        $crl = curl_init();
        $timeout = 5;
        curl_setopt ($crl, CURLOPT_URL,$url);
        curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
	curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
	$gotit=0;
	  $semaphore = new SyncSemaphore("Amazon");
	  $gotit = $semaphore->lock(1000);
          if (!$gotit) {
	    file_put_contents('/var/ywww/debug/phpDebug',
			      "Got it: ".(int)$gotit." 3\n",FILE_APPEND);
	    $gotit=$semaphore->lock(1000);
	    file_put_contents('/var/ywww/debug/phpDebug',
			      "Got it: ".(int)$gotit." 3a\n",FILE_APPEND);
	  }
        $ret = curl_exec($crl);
        curl_close($crl);
        usleep(500000);
	if ($gotit) {
	  $semaphore->unlock();
	}
        else {
	  file_put_contents('/var/ywww/debug/phpDebug',"W/o lock 3b\n",FILE_APPEND);
        }
        echo $ret;