Search code examples
phpsoapsoap-client

SoapClient connection to SoapServer


I need to connect to a SOAP Server from php, I read a lot of documentation, examples and tutorials, but I still cannot make authentication to my server. I have done the work below:

 $agencyNumber = 7818619810;
    $fullTransmission = false;
    //$context = array('header' => 'Content-type: application/soap+xml; charset=utf-8');
    $params = array( 'agencyNumber'     => 7818619810, 'fullTransmission' => 0/*,$context*/);

    $client = new SoapClient("http://link/to/server?wsdl");
   $test =  $client->__getFunctions();
    var_dump($test );// returns the functions my supplier provides, as well __getTypes() gives a bunch of variable arrays ect..
    $response = $client->__soapCall('GetTransmissionTicket',array($params) );//line 16 which is mentioned on error print
    var_dump($response);

Even though I set $context, when I try to run, I get the error below:

Fatal error: Uncaught SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. in C:\xampp\htdocs\xml\inc\connection_test.php:16 Stack trace: #0 [internal function]: SoapClient->__doRequest('http://interfac...', '..//my-provider...', 1, 0) #1 C:..path..test.php(16): SoapClient->__soapCall('GetTransmission...', Array) #2 {main} thrown in C:..path..test.php on line 16

The remote method I'm trying to call is called GetTransmissionTicket which takes two parameters, (int)agencyNumber and fullTransmission(bool)..

I want to emphasize that there are a lot of threads on this topic, some of which are really close to my question(1, 2, 3 and so on ..), but I really couldn't solve the problem. Please give a hand.. Kind Regards..


Solution

  • A simple example with soap and php can be

    $url="your WSDL url";
    $method = "Method you are calling";
    $error=0;
    $client = new SoapClient($url);
    
     try
      { 
        $info = $client->__call($method, array($param));
      } 
      catch (SoapFault $fault) 
     { 
        $error = 1; 
        errorReport($fault->faultcode,$fault->faultstring);
        die;
        /*echo '<script type="text/javascript">alert("Sorry,App Returne the following ERROR:'.$fault->faultcode."-".$fault->faultstring.'  We will now take you back to our homepage."); window.location = "'.$_SERVER['PHP_SELF'].'";</script> '; */
    
             }
    
         if($error==1)
          {
             $xml=$fault->faultstring;
         }else{
            $xml = $info;
         }
        return $xml;
    

    Try implementing it and let me know. if it works for you.