【问题标题】:Unable to send XML data using PHP sockets无法使用 PHP 套接字发送 XML 数据
【发布时间】:2013-08-30 05:37:54
【问题描述】:

我正在尝试通过使用 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);

【问题讨论】:

  • 可能只是一个错字,但请求的第一行错过了结束 '>'。
  • 糟糕,是的,这是一个错字,很抱歉,刚刚修复。我用来发送到我知道的服务的真实 XML 是有效的,因为我的开发环境可以使用 cURL 发送它没有问题。
  • 您能否使用 Wireshark 捕获通过 cURL 的请求和通过您的应用程序的请求并比较两者?
  • 我一直在使用 fiddler 来比较标题,据我所知它们是相同的。
  • 你能在 fiddler 中看到两个请求的正文吗?它们也一样吗?

标签: php xml web-services sockets soap


【解决方案1】:

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

..

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

..

【讨论】:

  • 非常感谢您的建议,我将代码改为使用\r\n而不是只使用\n,不幸的是,结果是一样的。
猜你喜欢
  • 1970-01-01
  • 2012-02-16
  • 2014-03-15
  • 2013-09-13
  • 2013-06-18
  • 2022-11-12
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多