使用SoapClient使用SOAP调用API - 无法找到方法

时间:2017-01-02 13:34:16

标签: php soap

我尝试连接到使用SOAP的API。它使用php / curl为我工作,但我真正想做的是使用SoapClient,它看起来会产生更清晰的代码。

这是有效的PHP:

$xml_post_string = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope
/" xmlns:v300="http://V300">
<soapenv:Header/>
   <soapenv:Body>
      <v300:GetNote soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <Request xsi:type="get:Request" xmlns:get="http://getNote.note.V300">
            <Security xsi:type="req:Security" xmlns:req="http://request.base.V300">
               <Password xsi:type="xsd:string">'.$soapPassword.'</Password>
               <Username xsi:type="xsd:string">'.$soapUser.'</Username>
            </Security>
            <NoteID xsi:type="xsd:string">'.$soapNoteID.'</NoteID>
         </Request>
      </v300:GetNote>
   </soapenv:Body>
</soapenv:Envelope>';

$headers = array(
                        "Content-type: application/soap+xml;charset=utf-8",
                        "Accept: text/xml",
                        "SOAPAction: https://webservices.aus.vin65.com/V300/NoteService.cfc?wsdl", 
                        "Content-length: ".strlen($xml_post_string),
                    ); 

$url = $soapUrl;


$ch = curl_init();
echo("set curl\n");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo("about to execute\n");
// converting
$response = curl_exec($ch); 
echo('Got '.strlen($response)." characters in response\n");
curl_close($ch);

echo("Response:\n".$response."\n");

这是我尝试使用SoapClient版本,由这位有用的StackExchange用户通知:How to make a PHP SOAP call using the SoapClient class

class Security{
        function Security($Username,$Password){
                $this->Username = $Username;
                $this->Password=$Password;
        }
}

class Request{
        function Request($Security,$NoteID){
                $this->Security = $Security;
                $this->NoteID = $NoteID;
        }
}

$client = new SoapClient($soapUrl);

$security = new Security($soapUser,$soapPassword);
$securityArray = array("Username"=>$soapUser,"Password"=>$soapPassword);

//$request = new Request($security,$soapNoteID);//gave error message "expects parameter 2 to be array"
//$request = array("Security"=>$security,"NoteID"=>$soapNoteID);//gave error message "No such operation 'GetNote'"        
$request = array("Security"=>$securityArray,"NoteID"=>$soapNoteID);//gave error message "No such operation 'GetNote'"

echo("Showing Request:\n");
var_dump($request);

$response=null;
try{
        $response = $client->__soapCall("GetNote",$request);
}catch(Exception $ex){
        echo("Caught Exception: ".$ex->getMessage()."\n");
        echo("Last Request was: ".$client->__getLastRequest()."\n");
        echo("Last Request Headers: ".$client->__getLastRequestHeaders()."\n");
        echo("Last Response was: ".$client->__getLastResponse()."\n");
        echo("Last Response Headers: ".$client->__getLastResponseHeaders()."\n");
}
if($response){
        var_dump($response);
}

正如您所看到的,我尝试了几种不同的方法来提供用户/密码/ NoteID信息,并记下每次给我的错误信息。目前,它声称没有这样的操作GetNote&#39; - 当然不对,但我不知道潜在的问题是什么。

提前感谢任何指示。

1 个答案:

答案 0 :(得分:1)

当我执行$ client-&gt; __ getFunctions()时,方法GetNote方法显示并正常工作

<?php
$client = new SoapClient('https://webservices.aus.vin65.com/V300/NoteService.cfc?wsdl');

/**
print_r($client->__getFunctions());
Array
(
    [0] => Response SearchNotes(Request $Request)
    [1] => Response AddUpdateNote(Request $Request)
    [2] => Response GetNote(Request $Request)
)
*/
$result = $client->GetNote([
    'Security' => [
        'Password' => 'XXX',
        'Username' => 'XXX'
    ],
    'NoteID' => 'XXX'
]);

print_r($result);