Can I http post xml using php?

D

designsimply

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

Martin Honnen

designsimply said:
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();[/QUOTE]

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');
 
S

Stefan Ram

designsimply said:
Array
(
Code:
 => 200
[headers] => Array
(
[Connection] => close
[connection] => close[/QUOTE]

What is the name of this markup language?

(I.e., the language defining this use of
"(", ")", "[", "]", and "=>".)
 
D

designsimply

Array
(
Code:
 => 200
[headers] => Array
(
[Connection] => close
[connection] => close[/QUOTE]

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

designsimply

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>";
 
D

designsimply

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top