?
Solved

PHP fsockopen causing Access Violation errors in IIS

Posted on 2008-10-29
6
Medium Priority
?
659 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 111

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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

752 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