469,285 Members | 2,561 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,285 developers. It's quick & easy.

Can I http post xml using php?

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.

Jul 20 '05 #1
5 11618


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();


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/
Jul 20 '05 #2
"designsimply" <sh**********@gmail.com> writes:
Array
(
[code] => 200
[headers] => Array
(
[Connection] => close
[connection] => close


What is the name of this markup language?

(I.e., the language defining this use of
"(", ")", "[", "]", and "=>".)
Jul 20 '05 #3
>Array
(
[code] => 200
[headers] => Array
(
[Connection] => close
[connection] => close


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.

Jul 20 '05 #4
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/packag...ttp-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/packag...lient.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/packag...aultheader.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/


Jul 20 '05 #5
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();

Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by turnit \(removethis\) | last post: by
6 posts views Thread by Phillip N Rounds | last post: by
3 posts views Thread by Paul M | last post: by
reply views Thread by WIWA | last post: by
5 posts views Thread by David Lozzi | last post: by
3 posts views Thread by JansenH | last post: by
1 post views Thread by Arfeen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.