failed to open stream: HTTP request failed!

2023-08-27 22:34 By "Powerless" 2737 0 1

近日突然在日志中发现报错:

......
[2023-08-27T10:08:27+08:00][error] [2]file_get_contents(http://www.example.com/): failed to open stream: HTTP request failed!HTTP/1.1 403 Forbidden
......

这个错误一般主要由下面几个问题导致:

    1、allow_url_fopen 值未设置为on

    2、allow_url_include 值未设置为on

    3、user_agent 未启用

随即查看机器配置如下:

[root@wzxaini9 /]# vim /usr/local/php/lib/php.ini
......
;;;;;;;;;;;;;;;;;;
; Fopen wrappers ;
;;;;;;;;;;;;;;;;;;

; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-fopen
allow_url_fopen = Off

; Whether to allow include/require to open URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-include
allow_url_include = On

; Define the anonymous ftp password (your email address). PHP's default setting
; for this is empty.
; http://php.net/from
;from="john@doe.com"

; Define the User-Agent string. PHP's default setting for this is empty.
; http://php.net/user-agent
; user_agent="PHP"
......

第一条和第三条都符合,随即做出两处修改

    1、allow_url_fopen 的值改为 On

    2、移除 user_agent="PHP" 前的 ;

结果如下

[root@wzxaini9 /]# vim /usr/local/php/lib/php.ini
......
;;;;;;;;;;;;;;;;;;
; Fopen wrappers ;
;;;;;;;;;;;;;;;;;;

; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-fopen
allow_url_fopen = On

; Whether to allow include/require to open URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-include
allow_url_include = On

; Define the anonymous ftp password (your email address). PHP's default setting
; for this is empty.
; http://php.net/from
;from="john@doe.com"

; Define the User-Agent string. PHP's default setting for this is empty.
; http://php.net/user-agent
user_agent="PHP"
......

修改保存后重启PHP服务

[root@wzxaini9 /]# service php-fpm reload

再次排查调用处,问题修复!

评 论

Others Discussion

  • PHP练习-移动数组内的0到最后并保持其他元素顺序不变
    Posted on 2020-08-14 20:32
  • PHP设计模式 - 委托模式
    Posted on 2019-04-25 16:15
  • HTTP头中隐藏PHP版本号
    Posted on 2021-01-11 16:38
  • Composer 异常 [ErrorException]
    Posted on 2019-11-25 17:55
  • PHP8.1 性能基准测试
    Posted on 2022-10-08 17:40
  • 必学十大经典排序算法,看这篇就够了
    Posted on 2019-11-18 16:30
  • Linux工具 - NM目标文件格式分析
    Posted on 2019-04-24 10:29
  • PHP练习-爬楼梯问题
    Posted on 2020-08-14 23:56