简体   繁体   English

PHP cURL无法连接到本地计算机上的IIS

[英]PHP cURL cannot connect to IIS on local machine

I have a PHP script that tries to connect with an other PHP script located on a IIS installation running on a local machine in my office using the cURL protocol but fails to do so. 我有一个PHP脚本试图使用cURL协议与位于我办公室中的本地计算机上的IIS安装上的其他PHP脚本连接,但无法这样做。

Local configuration: I've opened the port 8789 on the firewall, I made the router redirect correctly to the IP of the machine running IIS and I've set the default port of IIS to 8789. If I try to connect to the script (93.144.xxx.xxx:8987/curl_response.php) through a browser I get the resulting output so this setup is working correctly. 本地配置:我已打开防火墙上的端口8789,已将路由器正确重定向到运行IIS的计算机的IP,并将IIS的默认端口设置为8789。如果尝试连接到脚本( 93.144.xxx.xxx:8987/curl_response.php)通过浏览器得到最终的输出,因此此设置正常运行。

When I try to run the PHP script on a remote webserver though it gives my either cUrl error (#7): couldn't connect to host or cUrl error (#28): connect() timed out! 当我尝试在远程Web服务器上运行PHP脚本时,尽管它给出了我的cUrl错误(#7):无法连接到主机cUrl错误(#28):connect()超时! (I'm trying different webservers) (我正在尝试其他Web服务器)

The curl part of my PHP script is the following: 我的PHP脚本的curl部分如下:

$url = 'http://93.144.xxx.xxx:8789/curl_response.php';

$verbose = fopen('php://temp', 'w+');
$curl = curl_init();
curl_setopt ($curl, CURLOPT_VERBOSE, true);
curl_setopt ($curl, CURLOPT_STDERR, $verbose);
curl_setopt ($curl, CURLOPT_URL, $rurl);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt ($curl, CURLOPT_PORT , 8789);

$response = curl_exec($curl);

if ($response === FALSE) {
    printf("cUrl error (#%d): %s<br>\n", curl_errno($curl),
           htmlspecialchars(curl_error($curl)));
    rewind($verbose);
    $verboseLog = stream_get_contents($verbose);

    echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog),"</pre>\n";   
}else{
    curl_close($curl);
    // trim response (serialized array)
    $response = trim($response);
    // unserialize
    $response = unserialize($response);
    print_r($response);
}

I've tried several different CURLOPT_ configurations and I've even tried using a dyndns host instead of the IP, but to no avail. 我尝试了几种不同的CURLOPT_配置,甚至尝试使用dyndns主机代替IP,但无济于事。

Is there a IIS configuration to set so it will accept incomming cURL requests or is there a specific header to be sent though cURL? 是否需要设置IIS配置,使其能够接受传入的cURL请求,或者是否有通过cURL发送的特定标头?

Thanks. 谢谢。

You have a typo: 您有错字:

curl_setopt ($curl, CURLOPT_URL, $rurl);

Should be: 应该:

curl_setopt ($curl, CURLOPT_URL, $url);

Here's an example of a working curl request: 这是一个有效的 curl请求的示例:

<?php

$url = 'http://thepointless.com:80/';

$curl = curl_init();
curl_setopt ($curl, CURLOPT_VERBOSE, true);
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt ($curl, CURLOPT_PORT , 80);

$response = curl_exec($curl);

print $response;

?>

It's just like yours, except three things: 就像您一样,除了三件事:

  • It's connecting to port 80 正在连接端口80
  • It's not setting STDERR 这不是在设置STDERR
  • The typo is fixed 错字是固定的

Run cURL from the command line with --verbose to see more detailed information. 使用--verbose从命令行运行cURL以查看更多详细信息。

And check if ISS it's with cURL enabled: 并检查ISS是否启用了cURL:

extension=php_curl.dll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM