简体   繁体   English

PHP file_get_contents跨版本和操作系统的行为

[英]PHP file_get_contents behavior across versions and operating systems

I am using file_get_contents in PHP to get information from a client's collections on contentDM. 我在PHP中使用file_get_contents从contentDM上的客户端集合中获取信息。 CDM has an API so you can get that info by making php queries, like, say: CDM有一个API,因此您可以通过进行php查询来获取该信息,例如:

http://servername:port/webutilities/index.php?q=function/arguments http://服务器:端口/ webutilities / index.php的Q =功能/参数

It has worked pretty well thus far, across computers and operating systems. 到目前为止,它在计算机和操作系统上都运行良好。 However, this time things work a little differently. 但是,这次的工作方式有所不同。

http://servername/utils/collection/mycollectionname/id/myid/filename/myname http://服务器/ utils的/收集/ mycollectionname / ID /身份识别码/文件名/ MYNAME

For this query I fill in mycollection, myid, and myname with relevant values. 对于此查询,我用相关值填写mycollection,myid和myname。 myid and mycollection have to exist in the system, obviously. 显然,myid和mycollection必须存在于系统中。 However, myname can be anything you want. 但是,myname可以是您想要的任何名称。 When you run the query, it doesn't return a web page or anything to your browser. 运行查询时,它不会向您的浏览器返回网页或任何内容。 It just automatically downloads a file with myname as the name of the file, and puts it in your local /Downloads folder. 它只是自动下载一个以myname作为文件名的文件,并将其放在本地/ Downloads文件夹中。

I DON'T WISH TO DOWNLOAD THIS FILE. 我不想下载该文件。 I just want to read the contents of the file it returns directly into PHP as a string. 我只想读取文件的内容,它将它作为字符串直接返回到PHP。 The file I am trying to get just contains xml data. 我试图获取的文件仅包含xml数据。

file_get_contents works to get the data in that file, if I use it with PHP7 and Apache on my laptop running Ubuntu. 如果我在运行Ubuntu的笔记本电脑上将PHP_7和Apache一起使用,则file_get_contents可以获取该文件中的数据。 But, on my desktop which runs Windows 10, and XAMPP (Apache and PHP5), I get this error (I've replaced sensitive data with ###): 但是,在运行Windows 10和XAMPP(Apache和PHP5)的桌面上,出现此错误(我已用###替换了敏感数据):

Warning: file_get_contents(###/utils/collection/###/id/1110/filename/1111.cpd): failed to open stream: No such file or directory in D:\\Titus\\Documents\\GitHub\\NativeAmericanSCArchive\\NASCA-site\\api\\update.php on line 18 警告:file_get_contents(### / utils / collection / ### / id / 1110 / filename / 1111.cpd):无法打开流:D:\\ Titus \\ Documents \\ GitHub \\ NativeAmericanSCArchive \\ NASCA中没有此类文件或目录-site \\ api \\ update.php,第18行

My coworkers have been unable to help me so I am curious if anyone here can confirm or deny whether this is an operating system issue, or a PHP version issue, and whether there's a solid alternative method that is likely to work in PHP5 and on both Windows and Ubuntu. 我的同事无法帮助我,所以我很好奇这里是否有人可以确认或否认这是操作系统问题还是PHP版本问题,以及是否有可靠的替代方法可能在PHP5和两者中都适用Windows和Ubuntu。

file_get_contents() is a simple screwdriver. file_get_contents()是一个简单的螺丝刀。 It's very good for getting data by simply GET requests where the header, HTTP request method, timeout, cookiejar, redirects, and other important things do not matter. 这对于仅通过GET请求获取数据非常有用,而其中的标头,HTTP请求方法,超时,cookiejar,重定向和其他重要内容均无关紧要。

fopen() with a stream context or cURL with setopt are powerdrills with every bit and option you can think of. 具有流上下文的fopen()或具有setopt的cURL是您可以想到的每一个选项和功能的超级技巧。

In addition to this, due to some recent website hacks, we had to secure our sites more. 除此之外,由于最近一些网站被黑客入侵,我们不得不进一步保护我们的网站。 In doing so, we discovered that file_get_contents failed to work, where curl still would work. 这样做时,我们发现file_get_contents无法正常工作,而curl仍然可以正常工作。

Not 100%, but I believe that this php.ini setting may have been blocking the file_get_contents request. 不是100%,但我认为此php.ini设置可能已阻止了file_get_contents请求。

; Disable allow_url_fopen for security reasons
allow_url_fopen = 0

Either way, our code now works with curl. 无论哪种方式,我们的代码现在都可以使用curl。

reference : http://25labs.com/alternative-for-file_get_contents-using-curl/ http://phpsec.org/projects/phpsecinfo/tests/allow_url_fopen.html 参考: http : //25labs.com/alternative-for-file_get_contents-using-curl/ http://phpsec.org/projects/phpsecinfo/tests/allow_url_fopen.html

So, You can solve this problem by using PHP cURL extension. 因此,您可以通过使用PHP cURL扩展来解决此问题。 Here is an example that does the same thing you were trying: 这是一个与您尝试的操作相同的示例:

function curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }
$url = 'your_api_url';
$data = curl($url);

And finally you can check your data by print_r($data). 最后,您可以通过print_r($ data)检查数据。 Hope it you it will works and you will understand. 希望它会起作用,并且您会理解。

Reference : http://php.net/manual/en/book.curl.php 参考http : //php.net/manual/zh/book.curl.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM