file_get_contents()函数报500 Internal Server Error

本文探讨了在不同PHP版本下使用file_get_contents函数时遇到的问题及解决方案。作者在调试过程中发现,从PHP 5.3.10到5.4.7,此函数的行为有所变化。在低版本中,通过添加User-Agent header解决了599InternalServerError错误。
前几天,调试网站时发现,同样的代码,在两个不同的服务器上有不同的运行结果:一个运行结果没有任何问题,另外一个却显示599Internal Server Error。
于是,开始各种Debug,绕了一大圈才发现是file_get_contents()函数的问题,由于不同的php版本对其的支持不同。
没问题的php版本是5.4.7,
$jsonData = file_get_contents($url);
直接返回相应的结果

有问题的php版本是5.3.10
在网上搜了一下,碰到类似问题的人很多,推荐的方法是:
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n")); 
$context = stream_context_create($opts); 
$jsonData = file_get_contents($url,false,$context);
唯一不同的是,在请求url之前添加上了一个User-Agent header项,问题便顺利解决
网上给出解决方法的帖子中,也没能指出具体的原因。
现在想来,应该是低版本的php在进行http请求时必须添加http header,这样才符合http标准
而高版本的php则简化了该过程,已经提前配置兼容。

参考链接:
http://stackoverflow.com/questions/22498581/php-file-get-contents-500-internal-server-error-in-php
/** * @description: * @return {*} */ /* 发送错误响应 */ void send_error_response(int client_fd, http_status_t status) { const char *body = ""; switch (status) { case HTTP_400: body = "<h1>400 Bad Request</h1>"; break; case HTTP_403: body = "<h1>403 Forbidden</h1>"; break; case HTTP_404: body = "<h1>404 Not Found</h1>"; break; case HTTP_500: body = "<h1>500 Internal Server Error</h1>"; break; case HTTP_501: body = "<h1>501 Not Implemented</h1>"; break; default: /* 对于未知的状态码,包括HTTP_200,将其视为服务器内部错误 */ status = HTTP_500; body = "<h1>500 Internal Server Error</h1>"; break; } char response[512]; int len = snprintf(response, sizeof(response), "HTTP/1.1 %d %s\r\n" "Content-Type: text/html\r\n" "Content-Length: %zu\r\n" "Connection: close\r\n\r\n%s", status, (status == 400) ? "Bad Request" : (status == 403) ? "Forbidden" : (status == 404) ? "Not Found" : (status == 500) ? "Internal Server Error" : "Not Implemented", strlen(body), body); send(client_fd, response, len, 0); } /* 客户端处理函数 */ void *handle_client(void *arg) { int client_fd = *(int *)arg; free(arg); char buffer[MAX_REQUEST_SIZE]; ssize_t bytes_read = recv(client_fd, buffer, sizeof(buffer) - 1, 0); if (bytes_read <= 0) { close(client_fd); return NULL; } buffer[bytes_read] = '\0'; /* 解析HTTP请求 */ char method[16], path[256], protocol[16]; if (sscanf(buffer, "%15s %255s %15s", method, path, protocol) != 3) { send_error_response(client_fd, HTTP_400); close(client_fd); return NULL; } /* 只支持GET方法 */ if (strcmp(method, "GET") != 0) { send_error_response(client_fd, HTTP_501); close(client_fd); return NULL; } /* 构建文件路径 */ char full_path[MAX_PATH_LENGTH]; snprintf(full_path, sizeof(full_path), "%s%s", WEBROOT, (strcmp(path, "/") == 0) ? "/index.html" : path); /* 检查路径安全性 */ if (!is_path_safe(full_path)) { send_error_response(client_fd, HTTP_403); close(client_fd); return NULL; } /* 打开文件 */ int file_fd = open(full_path, O_RDONLY); if (file_fd < 0) { send_error_response(client_fd, HTTP_404); close(client_fd); return NULL; } /* 获取文件信息 */ struct stat file_stat; if (fstat(file_fd, &file_stat) < 0) { close(file_fd); send_error_response(client_fd, HTTP_500); close(client_fd); return NULL; } /* 发送文件 */ const char *content_type = get_content_type(full_path); send_http_response(client_fd, HTTP_200, content_type, file_stat.st_size); /* 使用零拷贝发送文件内容 */ off_t offset = 0; sendfile(client_fd, file_fd, &offset, file_stat.st_size); close(file_fd); close(client_fd); return NULL; } 为这两个函数生成头注释
最新发布
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值