【问题标题】:lumen- file_get_contents gives error of HTTP_INTERNAL_SERVER_ERRORlumen-file_get_contents 给出 HTTP_INTERNAL_SERVER_ERROR 错误
【发布时间】:2019-11-04 05:58:01
【问题描述】:

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

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

Symfony\Component\HttpKernel\Exception\HttpException 对象
(
    [状态代码:Symfony\Component\HttpKernel\Exception\HttpException:private] => 500
    [标题: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


【解决方案1】:

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

祝你好运

【讨论】:

    【解决方案2】:

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

    $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);
    

    祝你好运!

    【讨论】:

    • 感谢您的回答,我已经使用它,但没有解决,我发现了问题,问题是 url 中的空格,我使用了 urlencode,但它不起作用,我已将空格替换为 %20 和它奏效了
    猜你喜欢
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 2015-09-02
    • 2012-01-18
    相关资源
    最近更新 更多