Page 1 of 1
API - $_POST is null when using multiform/form-data
Posted: Mon Jan 09, 2012 7:34 pm
by YWSW
In the API tutorial multiform/form-data is used as the content-type. Then, the tutorial uses $_POST[''] to get the posts. This never seems to work for me as all my $_POST[' '] turn out null. A quick google search shows that many other people have this same issue (see,for example,
https://bugs.php.net/bug.php?id=26004), and I have not yet found a good solution.
Why was it working for the tutorial? What needs to be done?
Re: API - $_POST is null when using multiform/form-data
Posted: Mon Jan 09, 2012 9:07 pm
by Temor
YWSW wrote:In the API tutorial multiform/form-data is used as the content-type. Then, the tutorial uses $_POST[''] to get the posts. This never seems to work for me as all my $_POST[' '] turn out null. A quick google search shows that many other people have this same issue (see,for example,
https://bugs.php.net/bug.php?id=26004), and I have not yet found a good solution.
Why was it working for the tutorial? What needs to be done?
I had a similar problem with my host for quite some time where they accidentally blocked the server from processing post data. At least that's what I was told.
Is this on your local server or is someone else hosting for you?
Re: API - $_POST is null when using multiform/form-data
Posted: Mon Jan 09, 2012 9:13 pm
by YWSW
this is my own private cloud server (for which I have full control over). I can use $_POST everywhere on this server except when accessing this multipart/form-data content type used in the tutorial. It seems that everyone is having this issue- except for this tutorial here...
Re: API - $_POST is null when using multiform/form-data
Posted: Mon Jan 09, 2012 9:24 pm
by Temor
YWSW wrote:this is my own private cloud server (for which I have full control over). I can use $_POST everywhere on this server except when accessing this multipart/form-data content type used in the tutorial. It seems that everyone is having this issue- except for this tutorial here...
After some quick research I found that most people who experienced this problem simply had to up the cap on post_max_size or update apache to a later version or alternatively reinstall apache.
Re: API - $_POST is null when using multiform/form-data
Posted: Mon Jan 09, 2012 9:40 pm
by YWSW
true. but I am using apache 2.2.15 PHP 5.3.3 and I am not using a form to submit the info (I am using a raw http request as per the API tutorial- where he too did not create post_max_size field). In addition, I tested this out with a tiny post file and it still did not work...
Re: API - $_POST is null when using multiform/form-data
Posted: Mon Jan 09, 2012 9:47 pm
by jacek
It's more likely that you made a mistake with the request that is being sent than a problem with the server, can you post your code where you make the request ?
Re: API - $_POST is null when using multiform/form-data
Posted: Mon Jan 09, 2012 9:59 pm
by YWSW
$boundary = md5(microtime(true));
$post = '';
foreach($fs_request as $name => $value){
$post .= "--{$boundary}\r\n";
$post .= "Content-Disposition: form-data; name= \"{$name}\"\r\n\r\n";
$post .= "{$value}\r\n";
}
$post .= "--{$boundary}--\r\n";
if(isset($url['query']))
$head = "POST {$url['path']}?{url['query']} HTTP/1.1\r\n";
else
$head = "POST {$url['path']} HTTP/1.1\r\n";
$head .= "HOST: {$url['host']}\r\n";
$head .= "Content-Type: multipart/form-data; boundary= \"{$boundary}\"\r\n";
$head .= "Content-Length: ".strlen($post). "\r\n";
$head .= "Connection: close \r\n\r\n";
$socket = fsockopen($url['host'], ((isset($url['port'])) ? $url['port'] : 80));
fwrite($socket, "{$head}{$post}");
echo stream_get_contents($socket);
Re: API - $_POST is null when using multiform/form-data
Posted: Mon Jan 09, 2012 10:02 pm
by jacek
You have a extra space after name on this line
$post .= "Content-Disposition: form-data; name= \"{$name}\"\r\n\r\n";
I doube that will make it not work but it is worth a try. The same goes for boundary.
Also, you are missing a $ from the start of the second variable on this line
$head = "POST {$url['path']}?{url['query']} HTTP/1.1\r\n";
That is a bit more likely to cause problems.
Re: API - $_POST is null when using multiform/form-data
Posted: Mon Jan 09, 2012 10:09 pm
by YWSW
sorry, but it still does not work...
(you were correct about the missing $ but that was only if there was a GET which there was not)
Re: API - $_POST is null when using multiform/form-data
Posted: Wed Jan 11, 2012 12:23 am
by jacek
YWSW wrote:sorry, but it still does not work...
(you were correct about the missing $ but that was only if there was a GET which there was not)
Ah okay, Well next try adding an
echo $head, $post;
just before the fwrite. That might highlight where the request looks wrong, post it here if you are not sure still (view the page source in your browser to see the plain text)
Since you said you have full access to the server, have a look it Apache's error.log file, it should have logged a mroe detaied reason why the 500 was created.