如何使用 Laravel 和 PHP 将数据发送到 SOAP Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36575414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:35:50  来源:igfitidea点击:

How to send data to a SOAP Web Service using Laravel & PHP

phpxmllaravelcurlsoap

提问by jaahvicky

I trying to send an XML vacancies to a Web Service using Laravel and I'm struggling to figure out how to connect to the web service, authorise and send the required data.

我尝试使用 Laravel 将 XML 空缺发送到 Web 服务,但我正在努力弄清楚如何连接到 Web 服务、授权和发送所需的数据。

I have tried to use curl, but I am getting

我曾尝试使用 curl,但我得到了

Error: "" - Code: 0

错误:“” - 代码:0

Below is my Code

下面是我的代码

$result // IS MY XML file

$username = 'username'
$password = 'password'
$URL = 'http://xxxxxx.com;

//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"xmlRequest=" . $result);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_exec($ch);

if(!curl_exec($ch)){
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

curl_close($ch);

}

Another question that would be of help would be - Is there another way of sending data to a SOAP Web Service without having to use Curl?

另一个有帮助的问题是 - 是否有另一种无需使用 Curl 即可将数据发送到 SOAP Web 服务的方法?

Thank you in advance

先感谢您

回答by alepeino

Yes there is. Take a look at the SoapClientclass: You can use it like this:

就在这里。看看这个SoapClient:你可以这样使用它:

$options = [
    'trace' => true,
    'cache_wsdl' => WSDL_CACHE_NONE
];

$credentials = [
    'username' => 'username'
    'password' => 'password'
];

$header = new SoapHeader($NAMESPACE, 'AuthentificationInfo', $credentials);

$client = new SoapClient($WSDL, $options); // null for non-wsdl mode

$client->__setSoapHeaders($header);

$params = [
    // Your parameters
];

$result = $client->GetResult($params);
// 'GetResult' being the name of the soap method

if (is_soap_fault($result)) {
    error_log("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})");
}

There is thislibrary for Laravel. I tried it, then ended up writing my own wrapper.

Laravel有这个库。我尝试过,然后最终编写了自己的包装器。