I have a working .NET REST client code. I need to convert the .NET code into PHP. After a lot of time spent to convert the code I have no clue on how to proceed further. The relevant code in .NET is as below:
Public Function GetDemographicDetail(ByVal UID As Decimal) As DataSet
Dim DsResult As New DataSet
Dim proxyobject As WebProxy = New WebProxy("http://proxyserver:80/", True)
'Variable to hold the XML file.
Dim sample As String
'URL of the Webservice.
Dim serviceBaseUrl As String = "http://srdh.punjab.gov.in:80/searchservice/searchservice/searchService"
Dim req As WebRequest = WebRequest.Create(serviceBaseUrl)
req.Proxy = proxyobject
Dim resourceUrl As String = ""
Dim method As String = "POST"
Dim Result As String
'Fetch the UID's in Dataset.
Dim Photo() As Byte
Dim DOB As String
Dim DOY As String
'Generate the XML file attaching UID No.
If IsNumeric(RTrim(UID.ToString())) And (RTrim(UID.ToString().Length = 12)) Then
sample = "<advanceSearchParameters xmlns=""http://uidai.gov.in/SRDH/SearchResults"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><uid>" & UID & "</uid><userId>user</userId><password>pass</password></advanceSearchParameters>"
'Hit webservice and save the result in "Result"
Result = UseHttpWebApproach(serviceBaseUrl, resourceUrl, method, sample)
End If
Retu DsResult
End Function
Private Function UseHttpWebApproach(ByVal serviceUrl As String, ByVal resourceUrl As String, ByVal method As String, ByVal xmlRequestBody As String) As String
Dim responseMessage As String = Nothing
Dim request = TryCast(WebRequest.Create(String.Concat(serviceUrl)), HttpWebRequest)
If request IsNot Nothing Then
request.ContentType = "application/xml"
request.Method = method
End If
If method = "POST" AndAlso xmlRequestBody IsNot Nothing Then
Dim requestBodyBytes As Byte() = ASCIIEncoding.UTF8.GetBytes(xmlRequestBody.ToString())
request.ContentLength = requestBodyBytes.Length
Using postStream As Stream = request.GetRequestStream()
postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length)
End Using
End If
If request IsNot Nothing Then
Dim response = TryCast(request.GetResponse(), HttpWebResponse)
If response.StatusCode = HttpStatusCode.OK Then
Dim responseStream As Stream = response.GetResponseStream()
If responseStream IsNot Nothing Then
Dim reader = New StreamReader(responseStream)
responseMessage = reader.ReadToEnd()
End If
Else
responseMessage = response.StatusDescription
End If
End If
Retu responseMessage
End Function
The following is the converted code into PHP:
<?php
//Store your XML Request in a variable
$input_xml = '<advanceSearchParameters xmlns="http://uidai.gov.in/SRDH/SearchResults" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><uid>823631843375</uid><userId>user</userId><password>pass</password></advanceSearchParameters>';
$url = "http://srdh.punjab.gov.in:80/searchservice/searchservice/searchService";
$header_data = array(
"Content-Type: application/xml",
);
//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);
curl_setopt($ch, CURLOPT_POST, count($input_xml));
// Following line is compulsory to add as it is
curl_setopt($ch, CURLOPT_POSTFIELDS,
"xmlRequest=" . $input_xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
//curl_setopt($ch, CURLOPT_STDERR, fopen(diame(__FILE__).'/errorlog.txt', 'a'));
echo $data = curl_exec($ch);
curl_close($ch);
//convert the XML result into array
$array_data = json_decode(json_encode(simplexml_load_string($data)), true);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
?>
The output xml for the above php code is that I get as output is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><srdhSearchResults xmlns="http://uidai.gov.in/SRDH/SearchResults"/><pre>Array
(
)
</pre>
whereas the expected output should be an xml record retued from the server.
Please guide as to what I may be doing wrong. Note: The useame and password have been changed as the above url is a live one and a test user credentials caot be provided.
