redoing my authorize.net connectivity with fsockopen...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

redoing my authorize.net connectivity with fsockopen...

Post by Burrito »

I wrote something a few years back for the advanced integration method to authorize.net using cURL. It works perfectly and I wouldn't want to change anything except for the fact that on a new site that I'm building...their host won't enable cURL.

Obviously this will have to connect over SSL and I don't have any experience with fsockopen and SSL....frankly I'm not even sure if it can be done. If it can't can someone provide some other ideas of ways that I can post data entirely from the web server to a.net for AIM? If it can be done, can someone provide me with some guidance as to how I should start.

TIA,

Burr
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Looked at using Snoopy? I don't recall offhand if it supports SSL, but it does use fsockopen().
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

here's what I came up with as a first cut.

this requires that you have openssl enabled on your server.

it looks to be working as far as I can tell. I had to suppress the warnings on fgets() but I read that it's a bug within php and they still haven't fixed it

Code: Select all

<? 
$server= 'ssl://secure.authorize.net';
$url = '/gateway/transact.dll';
$content = "x_version=3.1&x_delim_data=True&x_login=Test&x_password=Test&x_relay_response=False&x_test_request=True";
$content .= "&x_delim_char=,&x_first_name=Bob&x_last_name=Jones&x_address=1234 main street&x_city=AnyTown&x_state=Ut";
$content .= "&x_zip=84403&x_country=US&x_description=Test Desc&x_card_num=4444444444444444&x_exp_date=0909&x_method=CC";
$content .= "&x_type=AUTH_CAPTURE&x_amount=9.95&x_currency_code=USD";
$content_length = strlen($content);
$headers = "POST $url HTTP/1.0\r\nContent-type: application/x-www-form-urlencoded\r\nHost: $server\r\nContent-length: $content_length\r\n\r\n";
$fp = fsockopen($server, 443, $errno, $errstr);
if (!$fp)
{
	echo "Could Not Establish Connection.";
	exit;
}
fputs($fp, $headers);
fputs($fp, $content);
$ret = "";
while (!feof($fp)) 
{
	$ret.= @fgets($fp, 1024);
}
fclose($fp);
$pattern = "/.*?content-length.*?\d+/mis";
$ret = preg_replace($pattern,"",$ret);
echo $ret;
?>
Post Reply