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

时间:2013-08-27 12:52:45

标签: 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);

1 个答案:

答案 0 :(得分:0)

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

...

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

...