API - $_POST is null when using multiform/form-data

Post here is you are having problems with any of the tutorials.
Post Reply
YWSW
Posts: 5
Joined: Mon Jan 09, 2012 7:28 pm

API - $_POST is null when using multiform/form-data

Post 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?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: API - $_POST is null when using multiform/form-data

Post 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?
YWSW
Posts: 5
Joined: Mon Jan 09, 2012 7:28 pm

Re: API - $_POST is null when using multiform/form-data

Post 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...
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: API - $_POST is null when using multiform/form-data

Post 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.
YWSW
Posts: 5
Joined: Mon Jan 09, 2012 7:28 pm

Re: API - $_POST is null when using multiform/form-data

Post 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...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: API - $_POST is null when using multiform/form-data

Post 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 ?
Image
YWSW
Posts: 5
Joined: Mon Jan 09, 2012 7:28 pm

Re: API - $_POST is null when using multiform/form-data

Post 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);
Last edited by jacek on Mon Jan 09, 2012 9:59 pm, edited 1 time in total.
Reason: code tags...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: API - $_POST is null when using multiform/form-data

Post 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.
Image
YWSW
Posts: 5
Joined: Mon Jan 09, 2012 7:28 pm

Re: API - $_POST is null when using multiform/form-data

Post 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)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: API - $_POST is null when using multiform/form-data

Post 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.
Image
Post Reply