【问题标题】:file_get_contents does not work on port 8282file_get_contents 在端口 8282 上不起作用
【发布时间】:2012-08-21 16:48:11
【问题描述】:

我得到了以下基本脚本,它发出一个基本的 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 但这也没有用。请问有什么帮助吗?谢谢..

编辑我添加了set_time_limit (500);,现在第二个错误当然消失了……但第一个错误仍然存​​在。

【问题讨论】:

  • 为什么在端口 8282 上?该地址是否响应浏览器中的此端口?如果没有,请尝试使用 Nmap ip 地址之一查看打开的端口
  • 我的网络服务器回答 8282 .. 如果我将 URL 打开为 http://****.com:8282/blabla 是的,webapge 在浏览器中打开。我不明白 nmap ip 吗?
  • 从报错信息来看,端口不是问题……
  • @feeela 如果我使用我们的常规网站 80,它可以工作...
  • 如果您确定端口打开正确,则无需使用nmap。我做了一些测试,我会回馈;)

标签: php post file-get-contents


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      来自 PHP 手册

      void set_time_limit (int $seconds)

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

      调用时,set_time_limit() 从零重新启动超时计数器。 也就是说,如果超时时间是默认的 30 秒,而 25 脚本执行的秒数,例如 set_time_limit(20) 的调用是 完成后,脚本将在超时之前运行总共 45 秒。 如果设置为零,则不施加时间限制。

      【讨论】:

      • 我明白,但作为一个 POST 请求,它不应该超过 30 秒...我的意思是最多 1 秒...
      【解决方案4】:

      试试这个:

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

      (尤其是连接:关闭位)

      另外:你能用 curl 复制命令行上的问题吗?

      【讨论】:

      • cURL 给我couldn't connect to host 但返回TRUE
      【解决方案5】:

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

      列出哪些端口允许 httpd: 管理端口-l | grep -w http_port_t

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

      【讨论】:

        猜你喜欢
        • 2011-10-01
        • 2016-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-19
        • 2013-10-02
        • 1970-01-01
        相关资源
        最近更新 更多