Solved

PHP fsockopen causing Access Violation errors in IIS

Posted on 2008-10-29
6
642 Views
Last Modified: 2013-12-13
Hi there,

I am trying to use fsockopen on a newly setup server running Windows 2003, IIS and PHP version 5.2.6. I can run this particular script once, but if I try to run it a second or third time, I get the following error:

PHP has encountered an Access Violation at 01B5B74E

I then tried to pin point in my code what was causing the error, and it seems to be the fsockopen command. To test this, I hopped on the PHP site, copied their example code for fsockopen and ran. Again, it worked the first time but caused the error the second time.

I have attached the code I am trying to run (directly from php.net/fsockopen). The segment in my own script is very similar, connecting to a server > getting some info then closing the socket.

Can someone please assist in explaining what could be causing this script to run once but fail on subsequent runs.

Thanks.
<?php
$fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
 
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

Open in new window

0
Comment
Question by:medfacit
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:v2Media
ID: 22829444
I believe it's a threading issue, especially if you're using an ssl socket. There's a bug report somewhere; I remember reading it at bugs.php.net a little while back.

As a work-around, why not try using Curl with the option CURLOPT_RETURNTRANSFER as true.
http://php.net/curl
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22830258
FWIW, Here is the fsockopen code from my PayPal IPN Script.  It works on my Linux servers. ;-)

Your workaround might include CURL, or if you need only GET and not POST, you can just do file_get_contents( - URL - )

HTH, ~Ray
<?php // paypal_ipn.php - custom PayPal IPN processor
 
// READ THE POST FROM PayPal AND ADD 'cmd'
$req		= 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
	$value		= urlencode(stripslashes($value));	// ENCODE FOR BOUNCE-BACK
	$req		.= "&$key=$value";
}
 
// POST BACK TO PayPal SYSTEM TO VALIDATE
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
 
 
// TEST FOR VERIFICATION
if (!$fp) { /* HTTP ERROR */ }
 
// HTTP OPEN - WRITE HEADER AND REQUEST
fputs ($fp, $header . $req);
 
// HTTP OPEN - READ PayPal RESPONSE, DISCARDING HEADERS TO THE VERY END
$paypal_reply 	= '';
$paypal_headers	= '';
while (!feof($fp)) {
	$paypal_reply	= fgets ($fp, 1024);
	$paypal_headers	.= $paypal_reply;
}
fclose ($fp);
 
// IF THIS IS TRULY A POST FROM PAYPAL, PROCESS ORDER NOTIFICATION
if (strcmp ($paypal_reply, "VERIFIED") == 0) { /* IT IS VERIFIED */ }

Open in new window

0
 

Author Comment

by:medfacit
ID: 22839838
Hi there,

I have tried your suggestion v2Media and still get the same Violation error. I have attached the code that is causing this error.

Thanks.
<?php
 
$url = "http://www.domain.com/target.php";
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL,$url);
 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"field=value");
 
$result= curl_exec ($ch);
curl_close ($ch);
print $result;
 
?>

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:medfacit
ID: 22839847
Should have mentioned that it works only once like fsockopen then causes the violation error. Exactly the same result would indicate a problem with the server/firewall?

Thanks.
0
 
LVL 19

Accepted Solution

by:
v2Media earned 500 total points
ID: 22841049
Perhaps run through php.ini configuration settings, extension files etc for your php install on IIS. It sounds like a misconfiguration of php.

php.net/install.windows
0
 

Author Comment

by:medfacit
ID: 22854847
Problem solved. It was a misconfiguration.
Reinstalled PHP seems to have fixed the problem.
Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question