Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 09:38 AM
designsimply
Guest
 
Posts: n/a
Default 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.

  #2  
Old July 20th, 2005, 09:38 AM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Can I http post xml using php?



designsimply wrote:[color=blue]
> 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/
  #3  
Old July 20th, 2005, 09:38 AM
Stefan Ram
Guest
 
Posts: n/a
Default Re: Can I http post xml using php?

"designsimply" <sheribigelow@gmail.com> writes:[color=blue]
>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 "=>".)


  #4  
Old July 20th, 2005, 09:38 AM
designsimply
Guest
 
Posts: n/a
Default Re: Can I http post xml using php?

>Array[color=blue]
>(
> [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.

  #5  
Old July 20th, 2005, 09:38 AM
designsimply
Guest
 
Posts: n/a
Default Re: Can I http post xml using php?

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:[color=blue]
>
> I don't know the API of HTTP_Client but you should make sure you set[/color]
the[color=blue]
> Content-Type header to application/xml before you post the XML as[/color]
only[color=blue]
> then the server knows it receives XML. Thus you need to find out how[/color]
you[color=blue]
> set a request header with that client and then do e.g.
> $x->setRequestHeader('Content-Type', 'application/xml');
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/[/color]

  #6  
Old July 20th, 2005, 09:38 AM
designsimply
Guest
 
Posts: n/a
Default Re: Can I http post xml using php?

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles