Reputation: 31
I making a port of a functionality from a working C# class to a PHP class I'm tryinhg to do a very simple task but the performance is different and I can't find the problem, maybe you guys can help.
This is the C# code (not mine, working as is, sensitive data hidden):
Stream requestStream = null;
HttpWebRequest webRequest = null;
StringBuilder postBuilder = null;
HttpWebResponse webResponse = null;
string requestUrl = null;
string cookieString = null;
byte[] rawPostData = null;
requestUrl = "https://some_server/Login";
webRequest = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
postBuilder = new StringBuilder();
postBuilder.Append("<?xml version=\"1.0\" ?>");
postBuilder.Append("<authorization>");
postBuilder.Append("<username><![CDATA[some_name]]></username>");
postBuilder.Append("<password><![CDATA[some_password]]></password>");
postBuilder.Append("<domain><![CDATA[some_domain]]></domain>");
postBuilder.Append("</authorization>");
rawPostData = Encoding.Default.GetBytes(postBuilder.ToString());
webRequest.ContentType = "text/xml";
webRequest.Accept = "*/*,text/xml";
webRequest.ContentLength = rawPostData.Length;
webRequest.Method = "POST";
webRequest.UserAgent = "dsaxess/special";
requestStream = webRequest.GetRequestStream();
requestStream.Write(rawPostData, 0, rawPostData.Length);
requestStream.Flush();
webResponse = (HttpWebResponse)webRequest.GetResponse();
Debug.WriteLine("Status: " + webResponse.StatusCode);
//Debug.Write(webRequest.Headers);
//Debug.Write(webResponse.Headers);
requestStream.Close();
requestStream = null;
postBuilder = null;
Debug.WriteLine("Done!");
This is my PHP Code:
$ch = curl_init();
$url = "https://some_server/Login";
$host = parse_url($url, PHP_URL_HOST);
//$path = parse_url($url, PHP_URL_PATH);
$xml_data = "<?xml version='1.0' ?>".
"<authorization>".
"<username><![CDATA[some_name]]></username>".
"<password><![CDATA[some_password]]></password>".
"<domain><![CDATA[some_domain]]></domain>".
"</authorization>";
$headers = array(
"Content-type: text/xml",
"Accept: */*,text/xml",
"User-Agent: dsaxess/special",
"Host: ".$host,
"Content-length: ".strlen($xml_data),
"Expect: 100-continue",
"Connection: Keep-Alive"
);
//var_dump($headers);
//var_dump($xml_data);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
var_dump(curl_getinfo($ch,CURLINFO_HTTP_CODE));
//var_dump($data);
curl_close($ch);
}
C# Code give Status OK and a valid authentication token. PHP Code give Status 401 and error: User does not exist.
I can't tell why is different. I compared the request and response header on both and everything look the same.
Please help.
Upvotes: 0
Views: 91
Reputation: 31
Solved it myself. Headers syntax was wrong. The correct syntax is:
$headers = array(
"Content-type" => "text/xml",
"Accept" => "*/*,text/xml",
"User-Agent" => "dsaxess/special",
"Host" => $host,
"Content-length" => strlen($xml_data),
"Expect" => "100-continue",
"Connection" => "Keep-Alive"
);
Upvotes: 2