php - 无法使用 PHP 套接字发送 XML 数据

标签 php xml web-services sockets soap

我正在尝试通过使用 SOAP 的代理连接到 Web 服务。问题是我不能使用 cURL,也不能添加任何模块来使这个 SOAP 消息更容易编写,我必须手动为消息创建 XML 并通过我的代理发送它。好消息是我实际上能够建立与 Web 服务的连接,唯一的问题是我收到的每个回复都说:“空消息 - 未收到数据”。谁能看到我的套接字在通过 XML 发送时做错了什么:

/*this is just striped of personal info, 
the XML I'm actually using is valid since 
on my development environment (which has cURL) 
will successfully get results with this XML*/

$request  = '<soapenv:Envelope>';
$request .= '<soapenv:Header>';
$request .=     '</soapenv:Header>';
$request .=     '<soapenv:Body>';
$request .=     '</soapenv:Body>';
$request .= '</soapenv:Envelope>';

$fp = fsockopen($proxy, 80, $errno, $errstr, 100);
if (!$fp) {
    echo "$errstr ($errno)<br />\r\n";
} else {
    $out  = "POST " . $path . " HTTP/1.1\r\n";
    $out .= "Host: " . $host . "\r\n";
    $out .= "Content-Length: " . strlen($request) . "\r\n";
    $out .= "Content-Type: text/xml; charset=utf-8\r\n";
    $out .= "SOAPAction: \"XXXXXXXXXXXXXX\"\r\n";
    $out .= "Accept: text/xml\n";
    $out .= "Proxy-Authorization: Basic $auth\r\n"; 
    $out .= "Connection: close\r\n\r\n";
    $out .= $request;
    $output = '';
    fwrite($fp, $out);
    while (!feof($fp)) {
        $output .= fgets($fp);
    }
    fclose($fp);
}
$output = trim(substr($output, strpos($output, "<?")));
print_r($output);

最佳答案

我在 Java 中使用了一些 HTTP,这可以帮助你。
标题字段由 CRLF "\r\n"分隔:

..

$out  = "POST " . $path . " HTTP/1.1\r\n";
$out .= "Host: " . $host . "\r\n";

..

关于php - 无法使用 PHP 套接字发送 XML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18466013/

相关文章:

javascript - XML 声明中的 NativeScript 全局事件处理程序?

java - 如何使用 CXF 在服务器端启用压缩?

php - 如何在 PHP 中编写 Web 服务?

php - 设置公共(public)类变量

php - 是否可以通过内连接查询Mysql

xml - XSL 根据属性重命名元素

java - 将xml文件数据存储在DB中

php - 在 PHP 中回显 JavaScript

php - HTML 表单未通过复选框

algorithm - 如何统计最后一秒、一分钟、一小时的请求数?