I'm trying to write a php script to send a http post request to a url. It doesnt seem to pass through for the server is not receiving it. Can anyone help?
<?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();
}
?>