我正在尝试使用 php 连接到 SAP Soap 服务。 我有 url(后面是 xml,但 ssl 已过期)、用户名和密码。在邮递员中它返回200,所以没问题。 导入参数应为field1、field2、field2。 我知道该方法必须从xml中获取,我们假设它是methodName。 它应该返回与客户相关的信息。
但是当使用php连接时,它返回警告:file_get_contents(url):无法打开流:HTTP请求失败! HTTP/1.1 401 未经授权。
我还使用 cURL 进行了测试,它返回 200,以及像 falsefalsetruenumbersfalsetrue 这样的串联文本,尽管我只输入了 $response = curl_exec($curl);和 var_dump($response);
这是我尝试连接的代码:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$wsdlUrl = "https://wdtst.xxxxxx";
$username = "xxx";
$password = "xxx";
// Basic authentication headers
$context = stream_context_create([
'https' => [
'header' => "Authorization: Basic " . base64_encode("$username:$password"),
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);
$response = file_get_contents($wsdlUrl, false, $context);
// print_r($response);
if ($response === false) {
$error = error_get_last();
echo "HTTP Request failed: " . $error['message'];
} else {
$xml = simplexml_load_string($response);
if ($xml !== false) {
$field1 = (string) $xml->field1;
$field2 = (string) $xml->field2;
$field3 = (string) $xml->field3;
$methodName = "methodName";
$options = [
'trace' => true,
'login' => $username,
'password' => $password,
];
try {
$client = new SoapClient($wsdlUrl, $options);
$response = $client->$methodName([
"field1" => $field1,
"field2" => $field2,
"field3" => $field3
]);
print_r($response);
} catch (SoapFault $e) {
// Handle SOAP errors
echo "SOAP Error: " . $e->getMessage();
}
} else {
echo "Failed to parse response XML.";
}
}
?>
请帮忙! (请原谅任何英语错误,我不是本地人)。
我无法在评论中发送这一大部分代码,所以我希望它能解决问题。将您的 $context 替换为错误代码,然后重试。
<?php
$context = stream_context_create([
'http' => [
'method' => 'GET',
'timeout' => 30,
'header' => "Authorization: Basic " . base64_encode($username . ":" . $password),
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);
?>