Can I http post xml using php?

Discussion in 'XML' started by designsimply, May 16, 2005.

  1. designsimply

    designsimply Guest

    I am trying to http post some xml to to a remote server using php. When
    I try to submit xml using PEAR's HTTP_Client::post() to the remote
    server, I get back the following "Invalid Document Format" error:

    Array
    (
    Code:
     => 200
    [headers] => Array
    (
    [Connection] => close
    [connection] => close
    [Server] => Microsoft-IIS/6.0
    [server] => Microsoft-IIS/6.0
    [X-Powered-By] => ASP.NET
    [x-powered-by] => ASP.NET
    [Content-Length] => 51
    [content-length] => 51
    [Content-Type] => text/html
    [content-type] => text/html
    [Cache-Control] => private, max-age=0
    [cache-control] => private, max-age=0
    [Date] => Mon, 16 May 2005 13:46:57 GMT
    [date] => Mon, 16 May 2005 13:46:57 GMT
    )
    
    [body] => Invalid Document Format
    
    Here is my php code:
    
    // Variables
    $url = "http://www.corporate-ir.net/ireye/xmlsub.asp";
    $data = "<ALERT_SUBSCRIPTION>
    <COMPANY CORPORATE_MASTER_ID="{valid id removed}">
    <MEMBERS>
    <MEMBER>
    <EMAIL_ADDRESS>{valid email removed}</EMAIL_ADDRESS>
    <ALERTS>
    <ALERT SUBSCRIBE="YES">IR-NEWS</ALERT>
    <ALERT SUBSCRIBE="YES">IR-EVENT</ALERT>
    <ALERT SUBSCRIBE="YES">IR-SEC</ALERT>
    <ALERT SUBSCRIBE="YES">IR-RPT</ALERT>
    <ALERT SUBSCRIBE="YES">IR-MEDIA</ALERT>
    </ALERTS>
    </MEMBER>
    </MEMBERS>
    </COMPANY>
    </ALERT_SUBSCRIPTION>";
    
    // Send xml data to initiate email alert sign-up.
    require_once('HTTP/Client.php');
    $c =& new HTTP_Client();
    $c->post('$url', $data');
    $response = $c->currentResponse();
    //echo $response['body'];
    echo "<pre>"; print_r($response); echo "</pre>"; // easier to see full
    response
    
    Here is a VB example from the documentation I was given, but I am
    trying to do it with php instead.
    
    Dim xmlObj      As MSXML2.XMLHTTP
    Dim URL         As String
    Dim response    As String
    Dim xmlDoc      As DOMDocument26
    
    Set xmlObj      = CreateObject("MSXML2.xmlhttp")
    Set xmlDoc      = New DOMDocument26
    URL             = "http://www.corporate-ir.net/ireye/xmlsub.asp"
    
    xmlDoc.Load "C:\Documents and Settings\jdoe\Desktop\alert.xml"
    
    xmlObj.open "POST", URL, False
    xmlObj.send xmlDoc.xml
    response = xmlObj.responseText 'view the value of response to see if
    the post was successful
    MsgBox "Done"
    
    End Sub
    
    Thank you very much for any help with this.
    designsimply, May 16, 2005
    #1
    1. Advertising

  2. designsimply wrote:
    > I am trying to http post some xml to to a remote server using php. When
    > I try to submit xml using PEAR's HTTP_Client::post() to the remote
    > server, I get back the following "Invalid Document Format" error:
    >
    > Array
    > (
    >
    Code:
     => 200
    >     [headers] => Array
    >         (
    >             [Connection] => close
    >             [connection] => close
    >             [Server] => Microsoft-IIS/6.0
    >             [server] => Microsoft-IIS/6.0
    >             [X-Powered-By] => ASP.NET
    >             [x-powered-by] => ASP.NET
    >             [Content-Length] => 51
    >             [content-length] => 51
    >             [Content-Type] => text/html
    >             [content-type] => text/html
    >             [Cache-Control] => private, max-age=0
    >             [cache-control] => private, max-age=0
    >             [Date] => Mon, 16 May 2005 13:46:57 GMT
    >             [date] => Mon, 16 May 2005 13:46:57 GMT
    >         )
    >
    >     [body] => Invalid Document Format
    >
    > Here is my php code:
    >
    > // Variables
    > $url = "http://www.corporate-ir.net/ireye/xmlsub.asp";
    > $data = "<ALERT_SUBSCRIPTION>
    > <COMPANY CORPORATE_MASTER_ID="{valid id removed}">
    > <MEMBERS>
    > <MEMBER>
    > <EMAIL_ADDRESS>{valid email removed}</EMAIL_ADDRESS>
    > <ALERTS>
    > <ALERT SUBSCRIBE="YES">IR-NEWS</ALERT>
    > <ALERT SUBSCRIBE="YES">IR-EVENT</ALERT>
    > <ALERT SUBSCRIBE="YES">IR-SEC</ALERT>
    > <ALERT SUBSCRIBE="YES">IR-RPT</ALERT>
    > <ALERT SUBSCRIBE="YES">IR-MEDIA</ALERT>
    > </ALERTS>
    > </MEMBER>
    > </MEMBERS>
    > </COMPANY>
    > </ALERT_SUBSCRIPTION>";
    >
    > // Send xml data to initiate email alert sign-up.
    > require_once('HTTP/Client.php');
    > $c =& new HTTP_Client();[/color]
    
    I don't know the API of HTTP_Client but you should make sure you set the
    Content-Type header to application/xml before you post the XML as only
    then the server knows it receives XML. Thus you need to find out how you
    set a request header with that client and then do e.g.
    $x->setRequestHeader('Content-Type', 'application/xml');
    
    
    
    --
    
    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, May 16, 2005
    #2
    1. Advertising

  3. designsimply

    Stefan Ram Guest

    "designsimply" <> writes:
    >Array
    >(
    >
    Code:
     => 200
    >    [headers] => Array
    >        (
    >            [Connection] => close
    >            [connection] => close[/color]
    
    What is the name of this markup language?
    
    (I.e., the language defining this use of
    "(", ")", "[", "]", and "=>".)
    Stefan Ram, May 16, 2005
    #3
  4. designsimply

    designsimply Guest

    >Array
    >(
    >
    Code:
     => 200
    >    [headers] => Array
    >        (
    >            [Connection] => close
    >            [connection] => close[/color]
    
    The formatting for the above block of text is how php prints out an
    array when you call print_r($array). See
    http://us2.php.net/manual/en/function.print-r.php. I called
    "print_r($response);" in my code to view how the server I want to post
    xml to is handling the request.
    designsimply, May 16, 2005
    #4
  5. designsimply

    designsimply Guest

    Should the content type for an http post of xml be text/xml or
    application/xml? Also, I am not sure how to set the header from within
    the HTTP_Client PEAR package.

    In the PEAR HTTP_Client documentation, I see that there are a couple
    functions that might help: HTTP_Client::head and
    HTTP_Client::setDefaultHeader().
    http://pear.php.net/manual/en/package.http.http-client.php

    The head function looks like a possibility, but the "string $url"
    syntax doesn't seem right for sending a content-type header.
    HTTP_Client::head (string $url)
    http://pear.php.net/manual/en/package.http.http-client.http-client.head.php

    I tried this function by putting the following, but I get the same
    "Invalid Document Format" error back.
    $c->setDefaultHeader('Content-Type', 'application/xml');
    HTTP_Client::setDefaultHeader (mixed $name [, string $value = NULL])
    http://pear.php.net/manual/en/package.http.http-client.http-client.setdefaultheader.php

    I also tried this.
    $c->setDefaultHeader('Content-Type', 'application/xml');

    And this.
    $default_headers = array('Content-Type' => 'application/xml');
    $c->setDefaultHeader($default_headers);

    Can someone help me with my PEAR HTTP_Client syntax?

    Here's my new code block that does not seem to be sending the proper
    xml content-type header.

    // Send xml data to initiate email alert sign-up.
    require_once('HTTP/Client.php');
    $c =& new HTTP_Client();
    $c->setDefaultHeader('Content-Type', 'application/xml');
    $c->post('$url', $data');
    $response = $c->currentResponse();
    echo "<pre>"; print_r($response); echo "</pre>";


    Martin Honnen wrote:
    >
    > I don't know the API of HTTP_Client but you should make sure you set

    the
    > Content-Type header to application/xml before you post the XML as

    only
    > then the server knows it receives XML. Thus you need to find out how

    you
    > set a request header with that client and then do e.g.
    > $x->setRequestHeader('Content-Type', 'application/xml');
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
    designsimply, May 16, 2005
    #5
  6. designsimply

    designsimply Guest

    With Justin Patrin's help (thanks Justin!), I figured out the solution
    for how to post an xml structure to a remote server using php. I used
    HTTP_Request and not HTTP_Client. Here is what I did:

    $req =& new HTTP_Request($url);
    $req->addHeader("Content-Type", "text/xml");
    $req->addHeader("Content-Length", strlen($data));
    $req->setMethod(HTTP_REQUEST_METHOD_POST);
    $req->addRawPostData($data, true);
    $req->sendRequest();
    echo $req->getResponseBody();
    designsimply, May 16, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    4,876
    Tor Iver Wilhelmsen
    Aug 29, 2005
  2. steven
    Replies:
    1
    Views:
    964
    Martin Honnen
    May 6, 2004
  3. Replies:
    6
    Views:
    760
    John J. Lee
    Jun 25, 2006
  4. Rajive Narain
    Replies:
    0
    Views:
    1,733
    Rajive Narain
    Sep 18, 2009
  5. Fred
    Replies:
    17
    Views:
    251
Loading...

Share This Page