lumen- file_get_contents提供HTTP_INTERNAL_SERVER_ERROR错误

问题描述 投票:0回答:2

流明,我的代码在下面以api响应的形式发送文件内容,对于某些文档来说,它工作正常,但对于某些文档,它给出了HTTP_INTERNAL_SERVER_ERROR错误

$content = base64_encode(file_get_contents($URL));

Symfony \ Component \ HttpKernel \ Exception \ HttpException对象([statusCode:Symfony \ Component \ HttpKernel \ Exception \ HttpException:private] => 500[headers:Symfony \ Component \ HttpKernel \ Exception \ HttpException:private] =>数组()
[message:protected] => HTTP_INTERNAL_SERVER_ERROR
[string:Exception:private] => 
[code:protected] => 0
[file:protected] => D:\xampp7.1\htdocs\PROJECT_NAME\app\Exceptions\Handler.php
[line:protected] => 76
[trace:Exception:private] => Array
    ( ...

是否有解决方案或需要设置php.ini veriable?

在网络错误日志中,它给我类似错误,

failed to open stream: HTTP request failed! HTTP/1.1 505 HTTP Version not supported

为了解决这个问题,我将在下面的帮助下获得文件内容,

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $data[$k]['doc_url']);
$data = curl_exec($ch);
curl_close($ch);
echo"<pre>";print_r($data);die;

它为我提供了没有文件连接的空输出

我也添加了下面的代码

$content = base64_encode(file_get_contents(urlencode($data[$k]['doc_url']))); $data[$k]['doc_content'] = "$content";

它给我的错误是,无法打开流:PATH中没有这样的文件或目录...

php laravel lumen internal-server-error
2个回答
0
投票

可能是您尝试访问的URL阻止了您尝试从其服务器获取内容的方式?也许您应该使用具有所有相关设置的CURL。我知道重复请求的最好方法是在chrome上打开开发人员控制台,然后在“ Newtork”选项卡上找到您尝试模仿的请求。右键单击->复制->复制为CURL(cmd),然后将其粘贴到此处以接收等效的PHP CURL代码:https://incarnate.github.io/curl-to-php/希望它能对您有所帮助,如果不能,那么您能否扩展您要到达的目的地?

祝你好运


0
投票

首先,您可以将上下文设置为这种请求:

$options = array(
    'http' => array(
        'protocol_version' => '1.0',
        'method' => 'GET'
    )
);
$context = stream_context_create($options);

尝试像这样获取文件的路径:

$api = "http://PATH_TO_YOUR_FILE?param=optional_query_params";
$response = file_get_contents($api, false, $context);

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.