Page 1 of 1

XML in variable for Web Service

Posted: Mon Aug 17, 2009 12:53 pm
by EddiRae
I am trying to pass an XML string into a web service. This is what the web service is expecting.

Here is the entire code for my function:

Code: Select all

<?php
require_once('nusoap.php');
 
function TRECSend($a, $b, $c, $d, $e, $f)
{
  $str = '<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Transmission><EdProviderData><DetailData><Detail>';
  $str = $str . '<IN_PROV>' . $a . '</IN_PROV> <IN_CRSE>' . $b. '</IN_CRSE> <IN_CDATE>' .$c . '</IN_CDATE> <IN_ZIP>' . $d . '</IN_ZIP>';
  $str = $str . '<IN_LICENSE>' . $e . '</IN_LICENSE> <IN_NAME>' . $f . '</IN_NAME> </Detail> </DetailData> </EdProviderData> </Transmission>';
 
  $wsdl="https://www.trec.state.tx.us/ProcessEdRoster/Service1.asmx?wsdl";
 
  include 'TREC_DevToken.php';
 
  $client=new soapclient($wsdl);
 
//  create header object and insert into headers
//  $header = new SoapHeader('https://www.trec.state.tx.us/ValidationSoapHeader/', 'DevToken', $DevToken, false);
  $header = array(new SoapHeader('https://www.trec.state.tx.us/ValidationSoapHeader/', 'DevToken', $DevToken, false));
 
//  $xmlReq = ('xmlReq' => $str);
  $xmlReq = array('xmlReq' => $str);
 
  try
  {
    $response = $client->__soapCall("ProcessRoster", $xmlReq, null, $header);
    return $response;
  }
  catch (SoapFault $exception)
  {
   return $exception;
   }
}
 
?>
The problem is on line 21. When I don't use the Array, then I get a parse error. When I do use the array, I get a Object Reference not found for line 25.

I don't see anything wrong. I did correct one variable earlier that was giving me the Object Reference. That was corrected on line 12.

Any help would be greatly appreciated.
Eddi Rae

Re: XML in variable for Web Service

Posted: Tue Aug 18, 2009 9:34 pm
by yacahuma
Do you want to use nusoap? I think, please someone correct me if I am wrong, that nusoap was a good idea when php did not have a good soap support, but all that changed in php5. I think you should use soap libraries in php 5 not nusoap libraries.

I know this has nothing to do with your error(or maybe it does....) I see at their website last libraries are from 2007.

Re: XML in variable for Web Service

Posted: Tue Aug 18, 2009 10:02 pm
by yacahuma
I also think this is a mistake

Code: Select all

 
$str = '<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
 
you do not backslach if you are using single quotes

it should be

Code: Select all

 
 $str = '<?xml version="1.0" encoding="utf-8"?>'
 
Although for your small test it may seem overkill you can try this little wsdl to php tool I created

http://www.stccorp.net/soapwriter/soapwriter.php

The tool will generate all the functions needed. After that you will do something like

Code: Select all

 
$service = new Service();
$roster = new ProcessRoster();
$roster->xmlReq = '<?xml version="1.0" encoding="utf-8"?>....REST HERE';
try
{
$service->ProcessRoster($roster);
    }
    catch (SOAPFault $f)
    {
        $request ="<pre>\n";
        $request .=  "Request :\n".htmlspecialchars($subscriber->soapClient->__getLastRequest()) ."\n";
        $request .= "Response:\n".htmlspecialchars($subscriber->soapClient->__getLastResponse())."\n";
        $request .= "</pre>";
 
       echo   $f->getMessage() . ' ' . $request;
       
    }
 

If you have auth header you will have to modify the class to add the header
you can modify the constructor and add something like this.

Code: Select all

 
     $ns = 'https://www.trec.state.tx.us/ValidationSoapHeader/';
       $AuthHeader = new ValidationSoapHeader();
      $AuthHeader->DevToken ='TOKEN HERE';
       $header =  new SoapHeader($ns,"ValidationSoapHeader", $AuthHeader,false);
       $this->soapClient->__setSoapHeaders(array($header));   
 
You can modify the constructors so you can do $AuthHeader = new ValidationSoapHeader('TOKEN HERE');

Re: XML in variable for Web Service

Posted: Wed Aug 19, 2009 9:59 am
by EddiRae
Where were you 3 weeks ago!!! Thank you for your post. I am not very familiar with PHP. In fact this is my first project using it. Any help would be greatly appreciated.

Here is the newest code. I am getting an error, though.

[Wed Aug 19 09:56:42 2009] [error] [client 127.0.0.1] PHP Fatal error: Class 'Service' not found in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\TREC.php on line 13

Code: Select all

<?php
function TRECSend($a, $b, $c, $d, $e, $f)
{
  $str = '<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Transmission><EdProviderData><DetailData><Detail>';
  $str = $str . '<IN_PROV>' . $a . '</IN_PROV> <IN_CRSE>' . $b. '</IN_CRSE> <IN_CDATE>' .$c . '</IN_CDATE> <IN_ZIP>' . $d . '</IN_ZIP>';
  $str = $str . '<IN_LICENSE>' . $e . '</IN_LICENSE> <IN_NAME>' . $f . '</IN_NAME> </Detail> </DetailData> </EdProviderData> </Transmission>';
  $str = '"' . $str . '"';
 
  include 'TREC_DevToken.php';
  
  $wsdl="https://www.trec.state.tx.us/ProcessEdRoster/Service1.asmx?wsdl";
 
  $service = new Service();
  $roster = new ProcessRoster();
  $roster->xmlReq = $str;
 
  $ns = 'https://www.trec.state.tx.us/ValidationSoapHeader/';
  $AuthHeader = new ValidationSoapHeader();
  $AuthHeader->DevToken =$DevToken;
  $header =  new SoapHeader($ns,"ValidationSoapHeader", $AuthHeader,false);
  $this->soapClient->__setSoapHeaders(array($header));
 
  try
  {
     $service->ProcessRoster($roster);
  }
  catch (SOAPFault $Message)
  {
    $request ="<pre>\n";
    $request .=  "Request :\n".htmlspecialchars($subscriber->soapClient->__getLastRequest()) ."\n";
    $request .= "Response:\n".htmlspecialchars($subscriber->soapClient->__getLastResponse())."\n";
    $request .= "</pre>";
 
    return   $Message->getMessage() . ' ' . $request;
  }
}
?>

Re: XML in variable for Web Service

Posted: Wed Aug 19, 2009 10:53 am
by yacahuma
The service is part of the code created using the tool I gave you. Go to the page. Enter your wsld address and the tool will generate the code.

Save the file and include the file in your code.

Re: XML in variable for Web Service

Posted: Wed Aug 19, 2009 11:14 am
by yacahuma
This is the class file

Code: Select all

 
<?php
class HelloWorld {
}
class HelloWorldResponse {
    var $HelloWorldResult;//string
}
class ProcessRoster {
    var $xmlReq;//string
    function __construct($xmlReq) {
        $this->xmlReq = $xmlReq;
    }    
}
class ProcessRosterResponse {
    var $ProcessRosterResult;//string
}
class ValidationSoapHeader {
    var $DevToken;//string
    function __construct($DevToken) {
        $this->DevToken = $DevToken;
    }
}
class TRECSoapClient {
    var $soapClient;
    
    private static $classmap = array('HelloWorld'=>'HelloWorld', 'HelloWorldResponse'=>'HelloWorldResponse', 'ProcessRoster'=>'ProcessRoster', 'ProcessRosterResponse'=>'ProcessRosterResponse', 'ValidationSoapHeader'=>'ValidationSoapHeader');
    
    function __construct($url = 'https://www.trec.state.tx.us/ProcessEdRoster/Service1.asmx?wsdl') {
        $this->soapClient = new SoapClient($url, array("classmap"=>self::$classmap, "trace"=>true, "exceptions"=>true));
        
        include 'TREC_DevToken.php'; //DONT KNOW WHAT IS HERE
        $ns = 'https://www.trec.state.tx.us/ValidationSoapHeader/';
        $AuthHeader = new ValidationSoapHeader($DevToken);
        $header = new SoapHeader($ns, "ValidationSoapHeader", $AuthHeader, false);
        $this->soapClient->__setSoapHeaders(array($header));
    
    }
    
    function HelloWorld($HelloWorld) {
        
        $HelloWorldResponse = $this->soapClient->HelloWorld($HelloWorld);
        return $HelloWorldResponse;
    
    }
    function ProcessRoster($ProcessRoster) {
        
        $ProcessRosterResponse = $this->soapClient->ProcessRoster($ProcessRoster);
        return $ProcessRosterResponse;
    
    }
    function HelloWorld($HelloWorld) {
        
        $HelloWorldResponse = $this->soapClient->HelloWorld($HelloWorld);
        return $HelloWorldResponse;
    
    }
    function ProcessRoster($ProcessRoster) {
        
        $ProcessRosterResponse = $this->soapClient->ProcessRoster($ProcessRoster);
        return $ProcessRosterResponse;
    
    }
}
 
 
?>
 

Code: Select all

 
<?php
include 'TRECSoapClient.php';
 
 function TRECSend($a, $b, $c, $d, $e, $f)
 {
   $xml = '<?xml version="1.0" encoding="UTF-8" ?><Transmission><EdProviderData><DetailData><Detail>';
  $xml .= '<IN_PROV>' . $a . '</IN_PROV> <IN_CRSE>' . $b. '</IN_CRSE> <IN_CDATE>' .$c . '</IN_CDATE> <IN_ZIP>' . $d . '</IN_ZIP>';
  $xml .=  '<IN_LICENSE>' . $e . '</IN_LICENSE> <IN_NAME>' . $f . '</IN_NAME> </Detail> </DetailData> </EdProviderData> </Transmission>';
 
  
    try
   {
      $client= new TRECSoapClient();
      $client->ProcessRoster(new ProcessRoster($xml));
   }
   catch (SOAPFault $f)
   {
       echo  $f->getMessage();
        return false; //or whatever you want to return
   }
 }
 ?>
 
I uploded the class file, change it to .php

See if that works

Re: XML in variable for Web Service

Posted: Wed Aug 19, 2009 11:26 am
by EddiRae
We are getting there. I am using the SoapHeader.

I am getting this error:
[Wed Aug 19 11:16:52 2009] [error] [client 127.0.0.1] PHP Fatal error: Using $this when not in object context in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\TREC.php on line 65

Here is my code as it is now:

Code: Select all

 <?php
class HelloWorld{}
class HelloWorldResponse
{
  var $HelloWorldResult;//string
}
class ProcessRoster
{
  var $xmlReq;//string
}
class ProcessRosterResponse
{
  var $ProcessRosterResult;//string
}
class ValidationSoapHeader
{
  var $DevToken;//string
}
class Service
{
  var $SoapClient;
  private static $classmap = array('HelloWorld'=>'HelloWorld'
          ,'HelloWorldResponse'=>'HelloWorldResponse'
          ,'ProcessRoster'=>'ProcessRoster'
          ,'ProcessRosterResponse'=>'ProcessRosterResponse'
          ,'ValidationSoapHeader'=>'ValidationSoapHeader');
          
  function __construct($url='https://www.trec.state.tx.us/ProcessEdRoster/Service1.asmx?wsdl')
  {
    $this->soapClient = new SoapClient($url,array("classmap"=>self::$classmap,"trace" => true,"exceptions" => true));
  }
  
  function HelloWorld($HelloWorld)
  {
    $HelloWorldResponse = $this->soapClient->HelloWorld($HelloWorld);
    return $HelloWorldResponse;
  }
 
  function ProcessRoster($ProcessRoster)
  {
    $ProcessRosterResponse = $this->soapClient->ProcessRoster($ProcessRoster);
    return $ProcessRosterResponse;
  }
}
 
function TRECSend($a, $b, $c, $d, $e, $f)
{
       $str = '<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Transmission><EdProviderData><DetailData><Detail>';
       $str = $str . '<IN_PROV>' . $a . '</IN_PROV> <IN_CRSE>' . $b. '</IN_CRSE> <IN_CDATE>' .$c . '</IN_CDATE> <IN_ZIP>' . $d . '</IN_ZIP>';
       $str = $str . '<IN_LICENSE>' . $e . '</IN_LICENSE> <IN_NAME>' . $f . '</IN_NAME> </Detail> </DetailData> </EdProviderData> </Transmission>';
       $str = '"' . $str . '"';
 
       include 'TREC_DevToken.php';
 
       $wsdl="https://www.trec.state.tx.us/ProcessEdRoster/Service1.asmx?wsdl";
 
       $service = new Service();
       $roster = new ProcessRoster();
       $roster->xmlReq = $str;
 
       $ns = 'https://www.trec.state.tx.us/ValidationSoapHeader/';
       $AuthHeader = new ValidationSoapHeader();
       $AuthHeader->DevToken =$DevToken;
       $header =  new SoapHeader($ns,"ValidationSoapHeader", $AuthHeader,false);
       $this->soapClient->__setSoapHeaders(array($header));
 
       try
       {
         $service->ProcessRoster($roster);
       }
       catch (SOAPFault $Message)
       {
         $request ="<pre>\n";
         $request .=  "Request :\n".htmlspecialchars($subscriber->soapClient->__getLastRequest()) ."\n";
         $request .= "Response:\n".htmlspecialchars($subscriber->soapClient->__getLastResponse())."\n";
         $request .= "</pre>";
         return   $Message->getMessage() . ' ' . $request;
       }
}
?>
 
Thank you for all your help!!
Eddi Rae

Re: XML in variable for Web Service

Posted: Wed Aug 19, 2009 1:53 pm
by yacahuma
please check out the code above

Re: XML in variable for Web Service

Posted: Wed Aug 19, 2009 3:10 pm
by EddiRae
Getting closer ... I am now getting this error from the SERVER:

Server was unable to process request. ---> Object reference not set to an instance of an object.Returned:

There is nothing in the error.log file that explains this.

What should I look for?

Re: XML in variable for Web Service

Posted: Fri Aug 28, 2009 9:25 am
by EddiRae
yacahuma,
Why would you have the "GetMessage"? There isn't a class that is in the SoapClient referencing to that. I think this is the only piece that I need to fix. It seems to be working so far.

Code: Select all

<?php
include 'TRECSoapClient.php';
 
function TRECSend($a, $b, $c, $d, $e, $f)
{
       $xml = '<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Transmission><EdProviderData><DetailData><Detail>';
       $xml = $xml . '<IN_PROV>' . $a . '</IN_PROV> <IN_CRSE>' . $b. '</IN_CRSE> <IN_CDATE>' .$c . '</IN_CDATE> <IN_ZIP>' . $d . '</IN_ZIP>';
       $xml = $xml . '<IN_LICENSE>' . $e . '</IN_LICENSE> <IN_NAME>' . $f . '</IN_NAME> </Detail> </DetailData> </EdProviderData> </Transmission>';
       $xml = '"' . $xml . '"';
 
       try
       {
         $client = new TRECSoapClient();
         $client->ProcessRoster(new ProcessRoster($xml));
       }
       catch (SOAPFault $Message)
       {
//         echo $Message->GetMessage();
         return   $Message->getMessage();
//         return false;
       }
}
?>

Re: XML in variable for Web Service

Posted: Sat Sep 26, 2009 12:38 pm
by EddiRae
yacahuma,
Sorry for such a long delay in responding to this post.
I have the function that looks like this (TREC.php):

Code: Select all

 
<?php
include 'TRECSoapClient.php';
 
function TRECSend($a, $b, $c, $d, $e, $f)
{
       $xml = '<?xml version="1.0" encoding="UTF-8" ?>';
       $xml = $xml . '<Transmission><EdProviderData><DetailData><Detail>';
       $xml = $xml . '<IN_PROV>' . $a . '</IN_PROV> <IN_CRSE>' . $b. '</IN_CRSE> <IN_CDATE>' .$c . '</IN_CDATE> <IN_ZIP>' . $d . '</IN_ZIP>';
       $xml = $xml . '<IN_LICENSE>' . $e . '</IN_LICENSE> <IN_NAME>' . $f . '</IN_NAME> ';
       $xml = $xml . '</Detail> </DetailData> </EdProviderData> </Transmission>';
       $xml = '"' . $xml . '"';
 
       try
       {
        $client = new TRECSoapClient();
         $client->ProcessRoster(new ProcessRoster($xml));
       }
       catch (SOAPFault $Message)
       {
//         echo $Message->GetMessage();
         return   $Message->getMessage();
//         return false;
       }
}
?>
 

I am still getting the message, "Returned: Server was unable to process request. ---> Object reference not set to an instance of an object."

I am not sure what I need to do now.