0
我想写一个PHP脚本发送一个http发布请求到一个URL。它似乎没有通过服务器没有收到它。谁能帮忙?PHP插座http后不工作
<?php
function postXMLToURL ($server, $path, $xmlDocument) {
$xmlSource = $xmlDocument;
$contentLength = strlen($xmlSource);
//$fp = fsockopen($server, 80);
$fp = fsockopen($server,8080);
fwrite($fp, "POST $path HTTP/1.0\r\n");
fwrite($fp, "Host: $server\r\n");
fwrite($fp, "Content-Type: application/xml\r\n");
fwrite($fp, "Content-Length: $contentLength\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n"); // all headers sent
fwrite($fp, $xmlSource);
$result = '';
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
return $result;
}
function getBody ($httpResponse) {
$lines = preg_split('/(\r\n|\r|\n)/', $httpResponse);
$responseBody = '';
$lineCount = count($lines);
for ($i = 0; $i < $lineCount; $i++) {
if ($lines[$i] == '') {
break;
}
}
for ($j = $i + 1; $j < $lineCount; $j++) {
$responseBody .= $lines[$j] . "\n";
}
return $responseBody;
}
$xmlDocument = new DomDocument($final_xml); //final_xml is my xml in a string
$result = postXMLtoURL("localhost", "/resources", $xmlDocument);
$responseBody = getBody($result);
$resultDocument = new DOMDocument();
$resultDocument->loadXML($responseBody);
header('Content-Type: application/xml');
echo $resultDocument->saveXML();
}
?>
是否有你使用套接字而不是cURL的原因? http://php.net/manual/en/book.curl.php – 2010-08-19 20:16:40
尝试通过一个'fwrite'调用发送所有头文件... – ircmaxell 2010-08-19 20:17:53
我使用的服务器没有安装cURL。这和使用套接字有很大的区别吗? 我试着在一次fwrite调用中发送所有头文件,没有任何改变。 – John 2010-08-19 20:22:59