文件获取内容返回 false

use*_*975 1 php mysql sql

所以我试图让用户验证他们是否拥有该域。所以我生成了一个文件并让他们将其上传到他们的网站。所以我必须验证它,所以我要做的是

file_get_contents($url.'/'.$token.'.html');
Run Code Online (Sandbox Code Playgroud)

这一切的回报是

bool(false)
Run Code Online (Sandbox Code Playgroud)

这是更多代码

$url = $_POST['url'];

//Get site info
$gin = $con->prepare("SELECT * FROM verify WHERE url = :url");
$gin->bindValue(':url', $url);
$gin->execute();

//Get token
$t = $gin->fetch(PDO::FETCH_ASSOC);
$token = $t['token'];
$url = $t['url'];


//Get content
var_dump(file_get_contents($url.'/'.$token.'.html'));
Run Code Online (Sandbox Code Playgroud)

我的表中有 3 列token,这是文件中的字符串。url显然这是网址,它的example.com格式。以及一个经过验证的列,它是1或0。有任何想法吗?

Hyd*_* B. 5

根据我从超过 100 万个域名获取第三方内容的经验,我不建议您使用,file_get_contents()因为此 PHP 函数无法处理页面重定向、需要有效用户代理的网站等。您遇到的问题可能是仅特定于某个域名。解决您的问题的更好方法是使用curl。

    function download_content($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Firefox 32.0");
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
Run Code Online (Sandbox Code Playgroud)

用法:

$returned_content = download_content('http://stackoverflow.com');
Run Code Online (Sandbox Code Playgroud)