Do a stantard HTTP post request using PHP

Posted on 2006-04-05
Last Modified: 2009-12-16
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 = ""
 ResponseURL = "http://"
 ' create xml
 ' create request header
 XMLRequest = XMLDoc.CreateElement("Request")
 XMLHeader = XMLDoc.CreateElement("Source")
 ' client details node
 XMLNode = XMLDoc.CreateElement("RequestorID")
 XMLNode.SetAttribute("Client", ClientID)
 XMLNode.SetAttribute("EMailAddress", EmailAddress)
 XMLNode.SetAttribute("Password", ClientPassword)
 ' client preferences node
 XMLNode = XMLDoc.CreateElement("RequestorPreferences")
 XMLNode.SetAttribute("Language", Language)
 XMLChildNode = XMLDoc.CreateElement("RequestMode")
 XMLChildNode.InnerText = "SYNCHRONOUS"
 ' create request body
 XMLBody = XMLDoc.CreateElement("RequestDetails")
 XMLNode = XMLDoc.CreateElement("MyRequest")
 XMLChildNode = XMLDoc.CreateElement("Item1")
 XMLChildNode.SetAttribute("Subitem1", "val1")
 XMLChildNode.SetAttribute("Subitem2", "val2")
 XMLChildNode = XMLDoc.CreateElement("Item2")
 XMLGrandChild = XMLDoc.CreateElement("Item3")
 XMLGrandChild.InnerText = "2005-12-01"
 XMLGrandChild = XMLDoc.CreateElement("Item4")
 XMLGrandChild.InnerText = "2"
 XMLChildNode = XMLDoc.CreateElement("item6")
 XMLGrandChild = XMLDoc.CreateElement("Item7")
 XMLGrandChild.SetAttribute("Code", "db")
 XMLGrandChild.SetAttribute("NumberOfRooms", "1")
 ' create xml document
 ' post document
 Dim encoding As New System.Text.ASCIIEncoding()
 Dim byte1 As Byte() = encoding.GetBytes(XMLDoc.OuterXml)
 Dim HttpWReq As System.Net.HttpWebRequest = _
 CType(System.Net.WebRequest.Create(InterfaceURL), 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?

Question by:balrom
    LVL 20

    Expert Comment

    by:Muhammad Wasif
    LVL 20

    Expert Comment

    by:Muhammad Wasif
    Hope this basic example help

    $url = "";
    $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);
    echo $result;
    LVL 15

    Accepted Solution

    try out this link might help

    It uses the fsockopen method to POST XML data

    Author Comment

    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.


    Author Comment

    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?

    LVL 15

    Expert Comment

    can you post your code??

    Author Comment


    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.

    LVL 15

    Expert Comment

    check this link it has the same bug solved

    Author Comment

    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 "".

       for do this i first open a socket to the host:
           $Conn = fsockopen ("", 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");



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now