简体   繁体   English

使用fileGetContents的正确方法是什么?

[英]What is the correct way to use fileGetContents?

There are so many ways to enter a URL that I just created an array and tried them all ... 输入URL的方法有很多,我刚刚创建了一个数组并尝试了所有方法...

This happens to be for domains only: 这恰好仅适用于域:

private function pageFoundGet( $domain )
{
    $types = array(
        "file_get - 1"=>$domain,
        "file_get - 2"=>'www.' . $domain,
        "file_get - 3"=>'http://www.' . $domain,
        "file_get - 4"=>'https://www.' . $domain,
        "file_get - 5"=>'http://' . $domain,
        "file_get - 6"=>'https://' . $domain
    );

    foreach ($types as $code => $value) {
        if ($this->domain_file = $this->fileGetContents( $value ))
        {
            $this->file_start_code = $code;
            return true;
        }
    }
    return false;
}

But what is the correct way ? 但是正确的方法是什么?

You have to provide the protocol and the domain. 您必须提供协议和域。 So in your case, you shouldn't use 1 and 2. 因此,就您而言,不应使用1和2。

Now it's important, that each of the variants 3-6 could show completly different content and each of them is correct. 现在重要的是,每个变体3-6可以显示完全不同的内容,并且每个变体都是正确的。 You could show two different pages for http and https and you could even show different content for www and non www urls. 您可以为http和https显示两个不同的页面,甚至可以为www和非www url显示不同的内容。

Usually, there's a redirect, which redirects every of these requests to the same location, but that is not neccessary. 通常,存在一个重定向,它将所有这些请求重定向到同一位置,但这不是必需的。 file_get_contents is smart enough to follow this redirects on his own, so you even don't need to know, that an redirect exists. file_get_contents非常聪明,可以独自执行此重定向,因此您甚至无需知道重定向是否存在。

What I usually do is: Just put the url in the browser and use that for file_get_contents. 我通常要做的是:将URL放在浏览器中,然后将其用于file_get_contents。

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

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