SoapClient connection to SoapServer

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 04:48:11

问题


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..


回答1:


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.




回答2:


Try $params = array( 'agencyNumber' => 7818619810, 'fullTransmission' => false);

instead of $params = array( 'agencyNumber' => 7818619810, 'fullTransmission' => 0);

OR

Use $client = new SoapClient("http://link/to/server?wsdl", array('soap_version' => SOAP_1_1));

because application/soap+xml is the content-type passed when using SOAP 1.2, text/xml is used with SOAP 1.1,

Reference : how to change content-type of request?




回答3:


Shouldn't the last line be var_dump($response); instead of var_dump($client);

Anyways, you can also try using this to get the result :

$response = $client->GetTransmissionTicket(array($params) );
var_dump($response);



回答4:


I resolved this problem by switching from SOAP 1.2 to SOAP 1.1:

$this->client = new SoapClient(
    $url,
    array(
        "trace" => TRUE,
        "exception" => 0,
        "soap_version" => SOAP_1_1,
        "cache_wsdl" => WSDL_CACHE_MEMORY,
        "local_cert" => 'mycert.pem',
    )
);



回答5:


I will answer my own question so that it helps someone one day. I used nusoap and changed the encoding to utf-8. The code snippet is below:

require_once "nusoap.php";
$client = new nusoap_client("http://interface.--Serivce-Supplier-Link/Service.svc?wsdl", "wsdl");
$client->soap_defencoding = 'UTF-8';

$result = $client->call("GetTransmissionTicket", array( 'agencyNumber'     => 13155, 'fullTransmission' => false));
var_dump($result);

Kind Regards




回答6:


may be little late but could help others. so here it goes: $params is already an array so you don't have to use array($params) in

$response = $client->__soapCall('GetTransmissionTicket',array($params) );

instead use simple $params while calling.check it like this

$response = $client->GetTransmissionTicket($params);

also use $client->__getTypes(); to verify params to be passed. use catch to good effect to trace faultstringand faultactor. at the end if still not getting solution check it with soapUI(a software).



来源:https://stackoverflow.com/questions/16740576/soapclient-connection-to-soapserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!