如何解决问题 :: file_get_contents(http://localhost:8085/assets/data/test.txt): 无法打开流: HTTP 请求失败
通过使用 CodeIgniter 4 框架,我开发了RESTful api,我需要访问文件(.json 和 .txt)来获取内容。但无法访问 php 内置函数 file_get_contents()。 有关更多详细信息,请检查随附的屏幕截图 API_curl_file_get_content_error.PNG
并且 test.txt 文件也可以通过相同的文件路径访问。欲了解更多详细信息,请检查截图Input-txt-file-content.png
注意: 1) test.txt 文件和各自的目录具有完全权限。 2) 开发环境: <---->Apache/2.4.47 (Win64) OpenSSL/1.1.1k PHP/8.1.2 <---->数据库客户端版本:libmysql - mysqlnd 8.1.2 <---->PHP版本:8.1.2 <---->CodeIgniter 4
Product.php(index 方法)
<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
class Product extends ResourceController
{
use \CodeIgniter\API\ResponseTrait;
public function index()
{
helper("filesystem");
ini_set('max_execution_time', 300);
$data['msg'] = "Product Index wizard";
$data['status'] = 200;
$file_path = base_url() . '/assets/data/test.txt'; // Read JSON
$json = file_get_contents($file_path, true);
$json_data = json_decode($json, true);
return $this->respond($data);
}
}
By using CodeIgniter 4 framework, I've developed RESTful api and there I need to access file (.json and .txt) to get content. But not able to access php inbuilt function file_get_contents().
For more details, pls check attached screenshot API_curl_file_get_content_error.PNG
And test.txt file is also accessible with same file path. For more details pls check screenshot Input-txt-file-content.png
NOTE : 1) test.txt file and respective directories have full permission.
2) Development environment :
<---->Apache/2.4.47 (Win64) OpenSSL/1.1.1k PHP/8.1.2
<---->Database client version: libmysql - mysqlnd 8.1.2
<---->PHP version: 8.1.2
<---->CodeIgniter 4
Product.php (index method)
<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
class Product extends ResourceController
{
use \CodeIgniter\API\ResponseTrait;
public function index()
{
helper("filesystem");
ini_set('max_execution_time', 300);
$data['msg'] = "Product Index wizard";
$data['status'] = 200;
$file_path = base_url() . '/assets/data/test.txt'; // Read JSON
$json = file_get_contents($file_path, true);
$json_data = json_decode($json, true);
return $this->respond($data);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
说明:
请记住,“http://localhost:8085/”很可能指向项目的文档根,通常是
/public路径。因此,除非“assets”文件夹位于
/public
路径中,file_get_contents("http://localhost:8085/assets/data/test.txt ");
将无法找到请求的服务器资源。解决方案:
由于您的文件资源 (test.txt) 位于本地文件系统上,
因此不要:
使用以下内容:
常量
ROOTPATH
附录:
我相信您还忘记将
$json_data
输出添加到Product::index
资源控制器方法中返回的$data
变量。Explanation:
Remember that "http://localhost:8085/" most likely points to the document root of the project, which is usually the
/public
path. So, unless the "assets" folder resides in the/public
path,file_get_contents("http://localhost:8085/assets/data/test.txt");
will fail to find the requested server resource.Solution:
Since your file resource (test.txt) is on the local filesystem,
Instead of:
Use this:
constant
ROOTPATH
Addendum:
I believe you also forgot to add the
$json_data
output to the returned$data
variable in theProduct::index
resource controller method.