<?php

class Default_SoapTestController extends Ec_Controller_DefaultAction
{
	public function indexAction()
    {
    	// disable layouts and renderers
    	$this->getHelper('viewRenderer')->setNoRender(true);
    	
    	// initialize server and set URI
    	$server = new Zend_Soap_Server(null,
    			array('uri' => 'http://'.$_SERVER['HTTP_HOST'].'/soap-test/wsdl'));
    	
    	// set SOAP service class
    	$server->setClass('Service_SoapExample');
    	
    	// handle request
    	$server->handle();
    }
    
    public function wsdlAutoAction()
    {
    	// disable layouts and renderers
    	$this->getHelper('viewRenderer')->setNoRender(true);
    	 
    	// set up WSDL auto-discovery
    	$wsdl = new Zend_Soap_AutoDiscover();
    	 
    	// attach SOAP service class
    	$wsdl->setClass('Service_SoapExample');
    	 
    	// set SOAP action URI
    	$wsdl->setUri('http://'.$_SERVER['HTTP_HOST'].'/soap-test/index');
    	 
    	// handle request
    	$wsdl->handle();
    }
    
    public function soapclientAction()
    {
    	//print_r($_SERVER);
    	// initialize SOAP client
    	$options = array(
    			'location' => 'http://chukouoms.cargo.com/soap-test/index',
    			'uri'      => 'http://chukouoms.cargo.com/soap-test/index'
    	);
    	 
    	try {
    		$client = new Zend_Soap_Client(null, $options);
    		$result = $client->getProduct('solar');
    		print_r($result);
    	} catch (SoapFault $s) {
    		die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring);
    	} catch (Exception $e) {
    		die('ERROR: ' . $e->getMessage());
    	}
    }
    
    public function serviceAction()
    {
    	$input = file_get_contents('php://input');
    	if(!empty($input)){
    		$uri = APPLICATION_PATH . '/../data/wsdl/ServiceForTest.wsdl';
    		$server = new SoapServer($uri, array('cache_wsdl' => WSDL_CACHE_NONE));
    		$server->setClass('Service_SoapExample');
    		$server->handle();
    	}else{
    		echo 'Invalid SOAP request';
    	}
    	exit;
    }
    
    public function wsdlAction()
    {
    	$host = $this->_request->getHttpHost();
    	header("Content-type: text/xml; Charset=utf-8");
    	$content = file_get_contents(APPLICATION_PATH . "/../data/wsdl/ServiceForTest.wsdl");
    	//$content = str_replace('www.domain.com', $host, $content);
    	echo $content;
    }
    
    
    
    public function clientAction()
    {	
    	$url = 'http://www.chukouoms.com/soap-test/wsdl';
    	$client = new SoapClient($url, array( 'trace' => 1 ));
    	print_r($client->__getFunctions());
    	echo '<br><br>';
    	
    	/*$params['appKey'] = 'you are my key';
    	$params['appToken'] = 'i am token';
    	$result = $client->sendGoodsResult($params);
    	var_dump($result);
    	
    	echo '<br><br>';*/
    	
    	$prodinfo['id'] = '10';
    	$prodinfo['username'] = 'solar';
    	$result = $client->getProduct($prodinfo);
    	var_dump($result);
    }
    
}