file_get_contents在端口8282上不起作用


file_get_contents does not work on port 8282

我得到了以下基本脚本,他们提出了一个基本的POST请求(我只想让它工作,然后我会添加更多的东西):

#   Variables
$URL = 'http://******:8282/api/incoming_shipment/';
$postdata = http_build_query(
    array(
        'contract_id'       => 'Showcare-R124276',
        'shipment_from'     => 'Montréal',
        'shipment_to'       => 'Chicago',
        'shipping_time'     => '2012-08-16 14:51:01',
        'tracking_cie'      => 'Poste Canada',
        'tracking_type'     => 'Standard',
        'tracking_number'   => 'EP645 9834 123 9773'
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
$context = stream_context_create($opts);
$result = file_get_contents($URL, FALSE, $context);
print_r($result);

结果给我:

Warning: file_get_contents(http://******:8282/api/incoming_shipment/) [function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in D:'Inetpub'hightechhandling'api'api_push_contract.php on line 31
Fatal error: Maximum execution time of 30 seconds exceeded in D:'Inetpub'hightechhandling'api'api_push_contract.php on line 31

但当我用浏览器浏览网页时,它能完美地工作。我试过cURL和fsocketopen,但也没用。请帮忙吗?谢谢

EDIT我添加了set_time_limit (500);,现在第二个错误当然已经消失了。。。但第一个仍然存在。

好的,找到问题了。太愚蠢了。问题是因为发出请求的服务器(PHP文件所在的位置)启用了防火墙,并且防火墙只允许端口21、22、803306和1433用于外部请求。

对于CentOS,使用file_get_contents()访问80以外端口上的URL在拒绝权限的情况下不起作用。只有当selinux设置为"禁用"或"允许"时,它才能工作。

来自PHP手册

void set_time_limit(int$秒)

设置允许脚本运行的秒数。如果是达到时,脚本返回一个致命错误。默认限制为30秒,或者(如果存在)中定义的max_execution_time值php.ini.

调用时,set_time_limit()会从零重新启动超时计数器。换句话说,如果超时是默认的30秒,而25脚本执行几秒钟后,调用set_time_limit(20)完成后,脚本将运行总共45秒,然后超时。如果设置为零,则没有时间限制。

试试这个:

'header' => implode("'r'n", array("Connection: close", "Content-type: application/x-www-form-urlencoded")),

(尤其是连接:闭合位)

还有:你能用curl在commanline上重复这个问题吗?

如果出于安全考虑不想禁用selinux,可以修改selinux策略以允许httpd侦听8282

要列出httpd允许的端口,请执行以下操作:semanage端口-l|grep-w http_port_t

要添加端口8282:semanage端口-a-t http_port_t-p tcp 8282