balrom
asked on
Do a stantard HTTP post request using PHP
Hi experts,
i need to build an interface to some web services based on standard HTTP post as communication Method.
I have a .net example here:
Dim XMLDoc As New System.Xml.XmlDocument()
Dim XMLRequest As System.Xml.XmlElement
Dim XMLHeader As System.Xml.XmlElement
Dim XMLBody As System.Xml.XmlElement
Dim XMLNode As System.Xml.XmlElement
Dim XMLChildNode As System.Xml.XmlElement
Dim XMLGrandChild As System.Xml.XmlElement
Dim ClientID As String
Dim Language As String
Dim EmailAddress As String
Dim ClientPassword As String
Dim InterfaceURL As String
Dim ResponseURL As String
' client settings
ClientID = "YourClienTID"
EmailAddress = "YourClientEmail"
ClientPassword = "YourClientPassword"
Language = "YourLanguage"
InterfaceURL = "http://www.mysite.com/api/RequestListenerServlet"
ResponseURL = "http:// www.mysite.com/receiveRequest.asp"
' create xml
' create request header
XMLRequest = XMLDoc.CreateElement("Requ est")
XMLHeader = XMLDoc.CreateElement("Sour ce")
' client details node
XMLNode = XMLDoc.CreateElement("Requ estorID")
XMLNode.SetAttribute("Clie nt", ClientID)
XMLNode.SetAttribute("EMai lAddress", EmailAddress)
XMLNode.SetAttribute("Pass word", ClientPassword)
XMLHeader.AppendChild(XMLN ode)
' client preferences node
XMLNode = XMLDoc.CreateElement("Requ estorPrefe rences")
XMLNode.SetAttribute("Lang uage", Language)
XMLChildNode = XMLDoc.CreateElement("Requ estMode")
XMLChildNode.InnerText = "SYNCHRONOUS"
XMLNode.AppendChild(XMLChi ldNode)
XMLHeader.AppendChild(XMLN ode)
' create request body
XMLBody = XMLDoc.CreateElement("Requ estDetails ")
XMLNode = XMLDoc.CreateElement("MyRe quest")
XMLChildNode = XMLDoc.CreateElement("Item 1")
XMLChildNode.SetAttribute( "Subitem1" , "val1")
XMLChildNode.SetAttribute( "Subitem2" , "val2")
XMLNode.AppendChild(XMLChi ldNode)
XMLChildNode = XMLDoc.CreateElement("Item 2")
XMLGrandChild = XMLDoc.CreateElement("Item 3")
XMLGrandChild.InnerText = "2005-12-01"
XMLChildNode.AppendChild(X MLGrandChi ld)
XMLGrandChild = XMLDoc.CreateElement("Item 4")
XMLGrandChild.InnerText = "2"
XMLChildNode.AppendChild(X MLGrandChi ld)
XMLNode.AppendChild(XMLChi ldNode)
XMLChildNode = XMLDoc.CreateElement("item 6")
XMLGrandChild = XMLDoc.CreateElement("Item 7")
XMLGrandChild.SetAttribute ("Code", "db")
XMLGrandChild.SetAttribute ("NumberOf Rooms", "1")
XMLChildNode.AppendChild(X MLGrandChi ld)
XMLNode.AppendChild(XMLChi ldNode)
XMLBody.AppendChild(XMLNod e)
' create xml document
XMLRequest.AppendChild(XML Header)
XMLRequest.AppendChild(XML Body)
XMLDoc.AppendChild(XMLRequ est)
' post document
Dim encoding As New System.Text.ASCIIEncoding( )
Dim byte1 As Byte() = encoding.GetBytes(XMLDoc.O uterXml)
Dim HttpWReq As System.Net.HttpWebRequest = _
CType(System.Net.WebReques t.Create(I nterfaceUR L), System.Net.HttpWebRequest)
HttpWReq.ContentType = "text/xml"
HttpWReq.ContentLength = XMLDoc.OuterXml.Length
HttpWReq.Method = "POST"
Dim StreamData As Stream = HttpWReq.GetRequestStream( )
StreamData.Write(byte1, 0, byte1.Length)
' get response stream
Dim HttpWRes As System.Net.HttpWebResponse = CType(HttpWReq.GetResponse , System.Net.HttpWebResponse )
Dim receiveStream As Stream = HttpWRes.GetResponseStream ()
How can i do the same with PHP?
Regards.
i need to build an interface to some web services based on standard HTTP post as communication Method.
I have a .net example here:
Dim XMLDoc As New System.Xml.XmlDocument()
Dim XMLRequest As System.Xml.XmlElement
Dim XMLHeader As System.Xml.XmlElement
Dim XMLBody As System.Xml.XmlElement
Dim XMLNode As System.Xml.XmlElement
Dim XMLChildNode As System.Xml.XmlElement
Dim XMLGrandChild As System.Xml.XmlElement
Dim ClientID As String
Dim Language As String
Dim EmailAddress As String
Dim ClientPassword As String
Dim InterfaceURL As String
Dim ResponseURL As String
' client settings
ClientID = "YourClienTID"
EmailAddress = "YourClientEmail"
ClientPassword = "YourClientPassword"
Language = "YourLanguage"
InterfaceURL = "http://www.mysite.com/api/RequestListenerServlet"
ResponseURL = "http:// www.mysite.com/receiveRequest.asp"
' create xml
' create request header
XMLRequest = XMLDoc.CreateElement("Requ
XMLHeader = XMLDoc.CreateElement("Sour
' client details node
XMLNode = XMLDoc.CreateElement("Requ
XMLNode.SetAttribute("Clie
XMLNode.SetAttribute("EMai
XMLNode.SetAttribute("Pass
XMLHeader.AppendChild(XMLN
' client preferences node
XMLNode = XMLDoc.CreateElement("Requ
XMLNode.SetAttribute("Lang
XMLChildNode = XMLDoc.CreateElement("Requ
XMLChildNode.InnerText = "SYNCHRONOUS"
XMLNode.AppendChild(XMLChi
XMLHeader.AppendChild(XMLN
' create request body
XMLBody = XMLDoc.CreateElement("Requ
XMLNode = XMLDoc.CreateElement("MyRe
XMLChildNode = XMLDoc.CreateElement("Item
XMLChildNode.SetAttribute(
XMLChildNode.SetAttribute(
XMLNode.AppendChild(XMLChi
XMLChildNode = XMLDoc.CreateElement("Item
XMLGrandChild = XMLDoc.CreateElement("Item
XMLGrandChild.InnerText = "2005-12-01"
XMLChildNode.AppendChild(X
XMLGrandChild = XMLDoc.CreateElement("Item
XMLGrandChild.InnerText = "2"
XMLChildNode.AppendChild(X
XMLNode.AppendChild(XMLChi
XMLChildNode = XMLDoc.CreateElement("item
XMLGrandChild = XMLDoc.CreateElement("Item
XMLGrandChild.SetAttribute
XMLGrandChild.SetAttribute
XMLChildNode.AppendChild(X
XMLNode.AppendChild(XMLChi
XMLBody.AppendChild(XMLNod
' create xml document
XMLRequest.AppendChild(XML
XMLRequest.AppendChild(XML
XMLDoc.AppendChild(XMLRequ
' post document
Dim encoding As New System.Text.ASCIIEncoding(
Dim byte1 As Byte() = encoding.GetBytes(XMLDoc.O
Dim HttpWReq As System.Net.HttpWebRequest = _
CType(System.Net.WebReques
HttpWReq.ContentType = "text/xml"
HttpWReq.ContentLength = XMLDoc.OuterXml.Length
HttpWReq.Method = "POST"
Dim StreamData As Stream = HttpWReq.GetRequestStream(
StreamData.Write(byte1, 0, byte1.Length)
' get response stream
Dim HttpWRes As System.Net.HttpWebResponse
Dim receiveStream As Stream = HttpWRes.GetResponseStream
How can i do the same with PHP?
Regards.
Hope this basic example help
$url = "http://www.mysite.com/api/RequestListenerServlet";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "a=3&b=5"); //data to post
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;
$url = "http://www.mysite.com/api/RequestListenerServlet";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "a=3&b=5"); //data to post
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks to all for your replies.
I think is more convenient the babuno5's approach(no curl required and no post vars to specify).
I need some time to do a test.
Regards.
I think is more convenient the babuno5's approach(no curl required and no post vars to specify).
I need some time to do a test.
Regards.
ASKER
Hi,
i've done a test, getting the following response:
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in mymodule.php
any idea?
Regards.
i've done a test, getting the following response:
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in mymodule.php
any idea?
Regards.
can you post your code??
ASKER
Babuno5,
there is the code:
// try connection to the server
$ConnectionServer = fsockopen($CFGURLTest, 80, $errno, $errstr, 30);
if ($ConnectionServer) {
...
} else {
...
}
The variable $CFGURLTest contain the address of service that receive post request.
After the fsockopen, the control skip to the else statement.
Regards
there is the code:
// try connection to the server
$ConnectionServer = fsockopen($CFGURLTest, 80, $errno, $errstr, 30);
if ($ConnectionServer) {
...
} else {
...
}
The variable $CFGURLTest contain the address of service that receive post request.
After the fsockopen, the control skip to the else statement.
Regards
check this link it has the same bug solved
http://bugs.php.net/bug.php?id=11058
http://bugs.php.net/bug.php?id=11058
ASKER
Thanks babuno,
Post a request using sockets works fine, in the previuos post i was using a wrong parm in the fsockopen funtion.
For clarity to other users:
I have to call the service with the url "http://www.myhost.com/myservice".
for do this i first open a socket to the host:
$Conn = fsockopen ("www.myhost.com", 80, $errno, $errstr, 30); // do not specify "http://"
then i put the post directive, specifying the service name:
fputs($Conn, "POST /myservice HTTP/1.0\r\n");
regards.
Post a request using sockets works fine, in the previuos post i was using a wrong parm in the fsockopen funtion.
For clarity to other users:
I have to call the service with the url "http://www.myhost.com/myservice".
for do this i first open a socket to the host:
$Conn = fsockopen ("www.myhost.com", 80, $errno, $errstr, 30); // do not specify "http://"
then i put the post directive, specifying the service name:
fputs($Conn, "POST /myservice HTTP/1.0\r\n");
regards.
http://www.php.net/curl