using header to POST
Moderator: General Moderators
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
using header to POST
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?
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?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: using header to POST
You cannot post with header("Location: "). Redirects are limited to GET.
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Re: using header to POST
I know this... i may not be clear to what is going on so here is the pseudo scenarioJohn Cartwright wrote:You cannot post with header("Location: "). Redirects are limited to GET.
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
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: using header to POST
What is this then? header($_POST)? You can't use header() to POST.kendall wrote: I know this... i may not be clear to what is going on so here is the pseudo scenario
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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: using header to POST
Your 2nd explanation didn't clarify anything for me.
My thoughts exactly.What is this then? header($_POST)?
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Re: using header to POST
sorry If i was not clear i was just using pseudo talk
but the code being used in myproxy.php
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
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?
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);
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);I'm wondering if If the ajax request is being rejected because of the cross browser policy? you think?
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Re: using header to POST
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 enoughAbraCadaver 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.
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...
Last edited by kendall on Tue Oct 20, 2009 3:42 pm, edited 1 time in total.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: using header to POST
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
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.
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- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: using header to POST
If your host cannot accomodate your requirements, then the choice is obvious. Sianara.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
Shared hosts/VPS hosts are a dime a dozen.
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Re: using header to POST
I myself thought so too until i was searching the php.net and found thisJohn 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.
http://www.php.net/manual/en/function.header.php#89447
turns out alot of payment gateways use this...I was like wow...
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: using header to POST
If that is the case, then like I said, make sure you implement the newlines properly.
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Re: using header to POST
Well i went to do a simple test... but now i get a prompt to download the php script
Im not sure how come its doing that
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: using header to POST
Try testing with a HTTP debugging tool, such as fiddler2, to see exactly what headers are being sent.kendall wrote:Well i went to do a simple test... but now i get a prompt to download the php scriptIm not sure how come its doing that
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: using header to POST
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
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: using header to POST
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.