来自远程客户端和SSL的file_get_contents

时间:2017-11-23 15:32:17

标签: php ssl laravel-5 file-get-contents

我有PHP Laravel5项目。我在控制器的操作中使用file_get_contents(),如下所示:

$production = json_decode(file_get_contents(url('/operation/get-production/'.$job->id)))->production;

上述路由不在身份验证和授权范围之内,上面的代码可以很好地从localhost 中运行。但是,当来自Internet的远程用户使用该代码时,会生成以下错误:

enter image description here

我没有使用IIS,服务器是Ubuntu linux上的Apache。我不知道为什么在这种情况下会出现SSL?我很确定提供给该功能的网址是 http://... 而不是https://...

3 个答案:

答案 0 :(得分:2)

您的生产网址可能会被迫使用https(这是一件好事)。

file_get_contents()不是通过https获取数据的最佳选择,但如果激活了php_openssl扩展并且allow_url_fopen设置为“on”,则可以从https获取内容。

由于你似乎没有有效的SSL证书:使用PHP Curl必须是一个更好的主意(http://php.net/manual/fr/book.curl.php),因为你可以用这个禁用SSL错误:

import web
__name__ = 'main2'


urls = ("/.*", "hello")
app = web.application(urls, globals())

class hello:
    def GET(self):
        return 'Hello, world1!'

if __spec__ is None:
    app.run()

(如果你信任这个网址:你可以这样做,不要在外部网站上这样做)

答案 1 :(得分:0)

听起来好像证书验证失败了,因为它没有根证书的参考文件来检查它。请参阅this Stack Overflow answer

答案 2 :(得分:0)

您有两种可能的情况:

可能的答案#1,如果您使用的是apache,请检查您的conf并查看您的域名是否强制所有呼叫都使用SSL

  • 或 -

可能的答案#2您需要为GET URL创建流并将标头发送到其他服务器:

<?php //Create a flow 
$opt = array(
'http'=>array(
'method'=>'GET',
'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3',
'header'=>'Accept-language: en\r\n' .
                 'Cookie: foo=bar\r\n’ ) ); 
$context = stream_context_create($opt); 
//Get external url response
$file = file_get_contents('http://www.example.com/some_file', false, $context); ?>