2013-08-27 14 views
2

我試圖通過使用SOAP的代理連接到Web服務。問題在於我無法使用cURL,並且我無法添加任何可以使這個SOAP消息更容易編寫的模塊,我必須手動創建消息的XML並通過代理髮送它。好消息是,我實際上能夠建立與Web服務的連接,唯一的問題是我收到的每一個響應都回復說:'空消息 - 沒有收到數據'。任何人都可以看到我的套接字如何通過XML發送錯誤:無法使用PHP套接字發送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); 
+0

也許只是一個錯字,但請求的第一行未命中的閉合 '>' 。 –

+0

糟糕,是的,這是一個錯字,對此感到抱歉,只是固定的。我用來發送到我知道的服務的真正的XML是有效的,因爲我的開發環境可以使用cURL發送它沒有問題。 – Greedfeed

+0

你可以通過cURL和請求通過你的應用程序捕獲Wireshark請求並比較兩者嗎? –

回答

0

我在Java中使用了一點HTTP,這可以幫助你。 頭字段由CRLF分隔爲 「\ r \ n」 個:

..

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

..

+0

非常感謝您的建議,我改變了代碼使用\ r \ n而不是\ n,不幸的是,結果是一樣的。 – Greedfeed