<?php

class StackExchange_Customer_AjaxController extends Mage_Core_Controller_Front_Action
{
    public function ajaxAction()
    {
		$siret = $this->getRequest()->getParam('q');
		$url = 'https://public.opendatasoft.com/api/records/1.0/search/?dataset=sirene&q='.$siret;

		//  Initiate curl
		$ch = curl_init();
		// Disable SSL verification
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		// Will return the response, if false it print the response
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		// Set the url
		curl_setopt($ch, CURLOPT_URL,$url);
		// Execute
		$result=curl_exec($ch);
		// Closing
		curl_close($ch);

		//$this->getResponse()->setHeader('Content-type', 'application/json');
		//echo $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
		$jsonArray = json_decode($result, true);
		$siretArray = array();
		//echo $jsonArray['nhits'].'<br>';
		if($jsonArray['nhits']){
			foreach($jsonArray['records'] as $data) {
				$siretArray[] = $data['fields']['siret'];
			}
		}
		if(in_array($siret, $siretArray))
		{
			$response = 1;
		}else{
			$response = 0;
		}
		echo $response;
		//print_r($response);
		exit;
    }
}