問題描述
我正在嘗試使用 cURL 執行 SOAP 函數(因為我在使用 SoapClient() 時遇到錯誤).
I am trying to execute an SOAP-function using cURL (because I get an error using the SoapClient().
這是我的代碼(已完成一半)
This is my code (that is halfway working)
$credentials = "username:pass";
$url = "https://url/folder/sample.wsdl";
$page = "/folder";
$headers = array(
"POST ".$page." HTTP/1.0",
"Content-type: text/xml;charset="utf-8"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: "customerSearch"",
"Authorization: Basic " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
$data = curl_exec($ch);
問題在于沒有執行 SOAP 操作.而且我還需要將參數傳遞給操作.這甚至可能嗎?
The problem is that the SOAP-action is not being performed. And I also need to pass arguments to the action. Is this even possible?
推薦答案
您需要指定 POST 的 cURL 選項,并設置請求的正文 - 如果您不發送正文,則 POST 請求沒有意義(更重要的是,它不是 SOAP).構建一個完整的 HTTP 請求標頭不會削減它.
You need to specify the cURL options to POST, and set the body of the request - if your not sending a body, there's no point in a POST request (and more importantly, it isn't SOAP). Building a complete HTTP request header just won't cut it.
$credentials = "username:pass";
$url = "https://url/folder/sample.wsdl";
$body = ''; /// Your SOAP XML needs to be in this variable
$headers = array(
'Content-Type: text/xml; charset="utf-8"',
'Content-Length: '.strlen($body),
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'SOAPAction: "customerSearch"'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
// Stuff I have added
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
$data = curl_exec($ch);
這篇關于使用 cURL 執行 SOAP的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!