<?php

	$cookies = dirname(__FILE__).'/cookies.txt';

	$postcodes = text_to_arr(dirname(__FILE__).'/postcodes.txt');
	
	$file_list_se10 = dirname(__FILE__).'/list-se10.txt';
	//clear the text file
	$fh_se10 = fopen($file_list_se10, 'w');
	fclose($fh_se10);
	
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1');
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($curl, CURLOPT_COOKIEFILE, $cookies);
	curl_setopt($curl, CURLOPT_COOKIEJAR, $cookies);
	curl_setopt($curl, CURLOPT_URL, 'https://maps.americanexpress.com/AUShopSmall');
	$result = curl_exec($curl);

	curl_setopt($curl, CURLOPT_URL, 'https://apigateway.americanexpress.com/bigdata/v1/merchantrecommendations/merchant/typeaheadsearch');
	curl_setopt($curl, CURLOPT_REFERER, 'https://maps.americanexpress.com/AUShopSmall');
	curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-AMEX-API-KEY: jlHo3BmPyRKflBgaml3UvhX0gltbTgwT', 'Content-type: application/json'));
	curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

	$fh_output = fopen('amex-output.csv', 'w');
	
	$line = 'lat'.','.
			'lon'.','.
			'se10'.','.
			'name'.','.
			'address1'.','.
			'address2'.','.
			'address3'.','.
			'address4'.','.
			'address5'.','.
			'city'.','.
			'state'.','.
			'zip'.','.
			'primary_cr_category'.','.
			'sub_category'.','.
			'category';
	
	fwrite($fh_output, $line."\r\n");
	
	foreach ($postcodes as $postcode) {
		
		$postcode_arr = split (',', $postcode);
		
		$data_string = '{"handler_name":"coral","page_size":500,"page_no":"1","sort_order":"distance","input_request_id":"QC9zz9wA","input_context_id":"ShopSmall","request_data":{"channel":37,"country":"AU","ids":null,"campaign_id":"AMAU0001","location":{"lat":'.$postcode_arr[1].',"lon":'.$postcode_arr[2].'},"user_local_ts":"2016-10-14 14:54:11","mode":"instore","intl_radius":"2","unit":"miles"}}';	 
		echo "$postcode_arr[0]\r\n";
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
		$result = curl_exec($curl);
		
		$result_arr = json_decode($result, true);
		$result_arr = $result_arr['response_data']['results'][0];
		
		$merchants = $result_arr['merchants'];
	
		
		foreach ($merchants as $merchant) {
			$list_se10 = text_to_arr($file_list_se10);
			$se10 = $merchant['se10'];
			
			if (in_array($se10, $list_se10)) {
				//se10 in the list
			} else {
				//se10 not in the list
				
				//write the line
				$merchant['lat'] = $merchant['location']['lat'];
				$merchant['lon'] = $merchant['location']['lon'];

				$line = addQuotes($merchant['lat']).','.
						addQuotes($merchant['lon']).','.
						addQuotes($se10).','.
						addQuotes($merchant['name']).','.
						addQuotes($merchant['address1']).','.
						addQuotes($merchant['address2']).','.
						addQuotes($merchant['address3']).','.
						addQuotes($merchant['address4']).','.
						addQuotes($merchant['address5']).','.
						addQuotes($merchant['city']).','.
						addQuotes($merchant['state']).','.
						addQuotes($merchant['zip']).','.
						addQuotes($merchant['primary_cr_category']).','.
						addQuotes($merchant['sub_category']).','.
						addQuotes($merchant['category']);
				
				fwrite($fh_output, $line."\r\n");
				
				//log the item number
				$fh_se10 = fopen($file_list_se10, 'a');
				fwrite($fh_se10, "{$merchant['se10']}\r\n");
				fclose($fh_se10);
			}
		}
	}

	fclose($fh_output);
	
	//for testing
	function test_write($out) {
		$fh = fopen('testhtml.html', 'w');
		fwrite($fh, $out);
		fclose($fh);
	}
	
	function text_to_arr($file_items) {
		if ($fh = fopen($file_items, 'r')) {
			$content = '';
			while ($line = fread($fh, 1024)) {
				$content .= $line;
			}
			fclose($fh);
			$items = preg_split("/[\n\r\t ]+/", $content, -1, PREG_SPLIT_NO_EMPTY);
		}
		return $items;
	}
	
	function addQuotes($str){
		return '"'.trim($str).'"';
	}
?>