Page 1 of 2

using header to POST

Posted: Tue Oct 20, 2009 1:08 pm
by kendall
hey guys,

I need some help with identifying a problem... I am using Ajax to post to a proxy on my server which uses header() to POST to a "mock" webservice on another server. the web services uses a header("Location") to redirect information BACK to my proxy. However I do not seem to be getting any response. doesn't seem like my header POST is reaching to the destination but thats still to be confirm...

when using header() to POST what is the usual result/ response? when developing a process using header() to POST what should I be aware of?

Re: using header to POST

Posted: Tue Oct 20, 2009 2:17 pm
by John Cartwright
You cannot post with header("Location: "). Redirects are limited to GET.

Re: using header to POST

Posted: Tue Oct 20, 2009 2:37 pm
by kendall
John Cartwright wrote:You cannot post with header("Location: "). Redirects are limited to GET.
I know this... i may not be clear to what is going on so here is the pseudo scenario
AJAX POST -> myproxy.php --- header($_POST) ---> web-service server -- process $_POST then header("Location: myprox.php?infor=data") ---> my server myproxy.php echos $_GET --> AJAX callBack

I'm not getting any responseText on the callBack

checks with the service indicate that it didnt process the $_POST so I realise maybe i am not passing the variables...hence i probably not setting the correct header information...So i'm trying to just do a simple request and response test and i am not getting anything

Re: using header to POST

Posted: Tue Oct 20, 2009 3:03 pm
by AbraCadaver
kendall wrote: I know this... i may not be clear to what is going on so here is the pseudo scenario
What is this then? header($_POST)? You can't use header() to POST.

The way to POST data with PHP in general is to build a proper post request, fsockopen() to the server and fputs() your data, then fgets() the response. Or, take your pick of classes or functions here: http://us3.php.net/manual/en/book.http.php which may be easier.

-Shawn

Re: using header to POST

Posted: Tue Oct 20, 2009 3:27 pm
by John Cartwright
Your 2nd explanation didn't clarify anything for me.
What is this then? header($_POST)?
My thoughts exactly.

Re: using header to POST

Posted: Tue Oct 20, 2009 3:32 pm
by kendall
sorry If i was not clear i was just using pseudo talk

but the code being used in myproxy.php

Code: Select all

 
//....in an if condition
header("POST $path HTTP/1.1\r\n" );
    header("Host: $host\r\n" );
    header("Content-type: application/x-www-form-urlencoded\r\n" );
    header("Content-length: " . strlen($data) . "\r\n" );
    header("Keep-Alive: 300");
    header("Connection  keep-alive");
    header($data);
 
where host and path is the webservice

the code on the webservice is

header("Location: http://" . $referer['host'] . $redirect_path . '?' . $query);

where referer['host'] and redirecth_path is the location of myrpoxy.php

thus myproxy.php has a

Code: Select all

 
//...in an if condition
header("Content-type: text/html" );
    header("Connection: close\r\n\r\n" );
    echo http_build_query($_GET);
note that all this is happening via AJAX

I'm wondering if If the ajax request is being rejected because of the cross browser policy? you think?

Re: using header to POST

Posted: Tue Oct 20, 2009 3:39 pm
by kendall
AbraCadaver wrote: The way to POST data with PHP in general is to build a proper post request, fsockopen() to the server and fputs() your data, then fgets() the response. Or, take your pick of classes or functions here: http://us3.php.net/manual/en/book.http.php which may be easier.
Tell me something as I was trying to build a portable platform but my stupid host provider has ftp fsockopen and curl disabled in certain websites and im tired of the red tape process that i need to go through to get them enabled...the header() approach to POSTING seems to have been compatible enough

what does (PECL pecl_http >= 0.1.0) ? mean...as I am not familiar with the PECL is this something ALWAYS compiled with PHP? to what version should i have ?


ok i researched and realize this is not compiled. sorry I cant use this if its not pre-compiled with PHP already...

Re: using header to POST

Posted: Tue Oct 20, 2009 3:40 pm
by John Cartwright
I'm not sure if you can issue a POST through header() functions, never seen or tried it. You are far better off using a library such as cURL. In fact, the more I think about it, it probably won't work because the example you have given would represent invalid headers.

Generally a POST request would look like

Code: Select all

POST /foo HTTP1.1\r\n
Host: domain.com\r\n
Content-type: application/x-www-form-urlencoded\r\n
Content-length: 1024\r\n\r\n
foo=bar
Pay extra attention to the extra newlines prior to the actual post data, and since you do not pass header() newlines (because it adds them internally), I'm not sure it's possible. Not to mention the post data is usually streamed through the socket and not send with the initial request, I highly doubt this is workable.

Re: using header to POST

Posted: Tue Oct 20, 2009 3:43 pm
by John Cartwright
kendall wrote:
AbraCadaver wrote:Tell me something as I was trying to build a portable platform but my stupid host provider has ftp fsockopen and curl disabled in certain websites and im tired of the red tape process that i need to go through to get them enabled...the header() approach to POSTING seems to have been compatible enough
If your host cannot accomodate your requirements, then the choice is obvious. Sianara.

Shared hosts/VPS hosts are a dime a dozen.

Re: using header to POST

Posted: Tue Oct 20, 2009 3:45 pm
by kendall
John Cartwright wrote:I'm not sure if you can issue a POST through header() functions, never seen or tried it. You are far better off using a library such as cURL. In fact, the more I think about it, it probably won't work because the example you have given would represent invalid headers.
I myself thought so too until i was searching the php.net and found this

http://www.php.net/manual/en/function.header.php#89447

turns out alot of payment gateways use this...I was like wow... 8O

Re: using header to POST

Posted: Tue Oct 20, 2009 3:46 pm
by John Cartwright
If that is the case, then like I said, make sure you implement the newlines properly.

Re: using header to POST

Posted: Tue Oct 20, 2009 3:57 pm
by kendall
Well i went to do a simple test... but now i get a prompt to download the php script :crazy: Im not sure how come its doing that

Re: using header to POST

Posted: Tue Oct 20, 2009 4:16 pm
by John Cartwright
kendall wrote:Well i went to do a simple test... but now i get a prompt to download the php script :crazy: Im not sure how come its doing that
Try testing with a HTTP debugging tool, such as fiddler2, to see exactly what headers are being sent.

Re: using header to POST

Posted: Tue Oct 20, 2009 4:27 pm
by AbraCadaver
You can chase this as long as you wish, but the fact remains that you cannot post with header(). It is used to send response headers to the client.

-Shawn

Re: using header to POST

Posted: Wed Oct 21, 2009 5:05 am
by Jenk
That snippet in the comment is flawed, as it is assuming you already have a connection open to $host. As AbraCadaver/Shawn is posting, you cannot POST to a new connection with header(), it is only for client responses.