2017-01-02 96 views
0

我試圖連接到使用SOAP的API。它使用php/curl工作,但我真正想做的是使用SoapClient,它看起來會產生更清晰的代碼。API - 無法找到方法

這是工作的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' - 絕對不對,但我不知道底層問題是什麼。

預先感謝任何指針。

回答

1

當我做$客戶 - > __ 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); 
+0

啊!我沒有意識到你可以直接調用它,因爲我複製的示例代碼使用了__soapCall。 –

+0

無論如何,是的,將__soapCall更改爲直接使用該功能非常有效,非常感謝 –