Share via


cURL works on CLI but not IIS

Question

Tuesday, November 16, 2010 6:07 PM

I'm running 5.2.14 with IIS on Windows 7. Used the Windows Platform Installer.

When I write a script that uses cURL or file_get_contents() and run it via the commandline, everything works great.

If I use a script in a web page that tries to use CURL or file_get_contents to get data from an external URL, the request always fails when I access the script via a browser on localhost. cURL returns a response code of 0. file-get_contents comes back with "Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known".

Yet the EXACT same file, when run from the commandline works fine.

php.exe -i returns no errors. phpinfo() shows the same active/activated cURL as in php.exe -i. There doesn't seem to be a problem with cURL itself, but that IIS or some security policy is stopping it from resolving and contacting the remote host when it's running inside a web script in IIS. Even trying to call a script via system or exec fails with the same problem.

I've got a feeling that the user IIS is running PHP as is being blocked from getting out. I just don't know where/how to lift that block.

All replies (5)

Thursday, November 18, 2010 12:34 AM

Could it be resolution or connection based rather than permissions maybe? Try adding the external domain in question to your hosts file and try again.


Thursday, November 18, 2010 10:45 AM

It's connection based. I've tried using an IP address to instead of a domain name in the URL and both cURL and file_get_contents() continued to fail, but with a different error a little farther along in the process. The resolution error is a symptom of the lack of network access (can't connect to DNS to resolve the hostname).

OTOH, they work absolutely fine when I run the same script via a command prompt. I also do not believe that it's the firewall as I disabled the firewall by stopping the Windows Firewall service and still had the same failures. Somewhere there seems to be a permission setting that is denying network access to the IIS identity it uses for running PHP scripts even before we get to the firewall.

You can see why I'm so stumped.


Thursday, November 18, 2010 5:34 PM

 

Yes, I see your point. As you can easily obtain the user and process that IIS is using when running your script, my suggestion from here would be to download Process Monitor from Systernals and create a couple of filters that only include events from these two knowns. From there start the capture going and run your script. At the very least you should get a better idea of what IIS is trying to do with it.


Wednesday, March 2, 2011 2:51 AM

Hi

I get the very same issue... have you been able to fix it? If yes, how?

I use Windows Server 2008 R2, enabled IIS, installed php 5.3.3 using FastCGI. All php pages work fine on the server, but I can't use the file_get_contents php function to retrieved pages from any url. Only urls on localhost can be retrieved with file_get_contents, but as soon as I enter any other url, could it be on the intranet or extranet, I get a timeout. I suspected firewall, thus I deactivated completely, but it does not change anything, I can't still use the file_get_contents. Of course, allow_php_fopen is set to ON in php.ini. I spent hours googleing without any success...


Friday, March 4, 2011 9:58 AM

Hi all

After hours of search... I finally found. Hopefully, this will help you... file_get_contents does not use the proxy settings defined in Internet Explorer... therefore, you must set the context parameter manually, as below:

$opts = array('http' =>
    array(
        'proxy'  => your.proxy:8080'
    )
);
$context  = stream_context_create($opts);

echo file_get_contents($url, false, $context);