• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTTP POST from Controller, NOT from HTML Form

#1
[eluser]tkaw220[/eluser]
Goody day.

I am currently working on an eCommerce project. I wish to forward all the information POSTed from my HTML form to payment gateway, through my Controller. Now I am using cURL to do this. But the problem is that the payment gateway does not accept information POST from my server, what it asks is direct HTTP POST.

How do I perform normal HTTP POST (without cURL) in this case?

NOTE: I had tried the method given in this blog, http://wezfurlong.org/blog/2006/nov/http...hout-curl/, but it is not working.

Thanks for the help.

Have a nice day.

#2
[eluser]skunkbad[/eluser]
[quote author="tkaw220" date="1303550266"]Goody day.

I am currently working on an eCommerce project. I wish to forward all the information POSTed from my HTML form to payment gateway, through my Controller. Now I am using cURL to do this. But the problem is that the payment gateway does not accept information POST from my server, what it asks is direct HTTP POST.

How do I perform normal HTTP POST (without cURL) in this case?

NOTE: I had tried the method given in this blog, http://wezfurlong.org/blog/2006/nov/http...hout-curl/, but it is not working.

Thanks for the help.

Have a nice day.[/quote]

I've never heard of a gateway not accepting posts from the server. Are you sure? Maybe you just need to include a user agent string in your server post.

#3
[eluser]toopay[/eluser]
[quote author="tkaw220" date="1303550266"]How do I perform normal HTTP POST (without cURL) in this case?[/quote]

Without cURL? use fsockopen() function.

#4
[eluser]Sudz[/eluser]
[quote author="tkaw220" date="1303550266"]Goody day.

I am currently working on an eCommerce project. I wish to forward all the information POSTed from my HTML form to payment gateway, through my Controller. Now I am using cURL to do this. But the problem is that the payment gateway does not accept information POST from my server, what it asks is direct HTTP POST.

How do I perform normal HTTP POST (without cURL) in this case?

NOTE: I had tried the method given in this blog, http://wezfurlong.org/blog/2006/nov/http...hout-curl/, but it is not working.

Thanks for the help.

Have a nice day.[/quote]

#5
[eluser]Sudz[/eluser]
So, here's an example of how to send a POST request with straight up PHP, no cURL:

<?php
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}

$optional_headers is a string containing additional HTTP headers that you would like to send in your request.

PHP's HTTP wrapper will automatically fill out the Content-Length header based on the length of the $data that you pass in. It will also automatically set the Content-Type to application/x-www-form-urlencoded if you don't specify one in the $optional_headers.

I find this very handy; I don't need to code in redirection logic, HTTP auth handling, user agent setting and so on; they are handled for me by PHP. This works for HTTPS as well, if you have openssl enabled.

You may also want to look into http_build_query() which is a convenience function that allows you to assemble query/post parameters from a PHP variable, applying appropriate escaping.

#6
[eluser]toopay[/eluser]
Ha! Of course. I've just noticed that(DOH!), fopen() is easier programming style. Using a socket(fsockopen()), you will have to send the HTTP request by hand, but... it allows you to easily define a timeout (better error handler built in too) and it is more attractive(and fun, in my opinion), also typically you will use a socket for communication with rather unknown daemons/services (i.e. self-made).

#7
[eluser]skunkbad[/eluser]
My question is, how can the gateway determine your connection method? I don't believe it can. I think you just aren't supplying what it wants. If you can use fopen or fsockopen, there's no reason why you can't use cURL. The gateway is probably refusing all connections that don't have a user agent, has some sort of redirect procedure, or perhaps it forces cookie usage. Regardless, all of this can be handled by cURL.

#8
[eluser]markscutts[/eluser]
I think that you must set the correct header and then the gateway can detect you like a customer.

I advice that you put a user-agent (browser) header.

#9
[eluser]tkaw220[/eluser]
Hi,

@skunkbad:
I have no idea about how they determine my connection method. But according to them, another customer who use ASP.net framework (POSTing data to their gateway from controller also), encounter the same issue. Thus, they believe the problem derives from the cURL method.

@Sudhakar Prajapati:
Thanks for the code example. I will try this and see if it works.

@markscutts:
I tried to workaround this. But when I look at PHP manual about CURLOPT_USERAGENT, it does not provide string example for this option. Any idea?

#10
[eluser]markscutts[/eluser]
Here is an example:

curl_setopt($cURL, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; es-ES; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7");

Also, you can see your own user agent string on your browser ;-)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.