简体   繁体   English

PHP - file_get_contents() 在 localhost 但在实时服务器上完美运行

[英]PHP - file_get_contents() works perfectly on localhost but on live server

I have been trying to get the html content from a webpage using file_get_contents().我一直在尝试使用 file_get_contents() 从网页中获取 html 内容。 It works perfectly on localhost but not on live server.它可以在本地主机上完美运行,但不能在实时服务器上运行。

Some more information is as follows:更多信息如下:

  • PHP version is 7+ PHP 版本为7+
  • http, https are included in Registered PHP Streams http、https 包含在已注册的 PHP 流中
  • allow_url_fopen is On allow_url_fopen 开启
  • used cURL, but it redirects to some other page and uses my domain as the referrer.使用 cURL,但它重定向到其他页面并使用我的域作为引荐来源。
  • executing this in the browser by putting the code in a php file通过将代码放入 php 文件中,在浏览器中执行此操作
  • output of the below code on live server is false以下代码在实时服务器上的 output 是false
$url = 'https://trafficsecrets.com/ts-free-book';

$context = stream_context_create(
    array (
        'http' => array (
            'method' => 'GET',
            'follow_location' => true,
            'max_redirects' => 5,
            'header' => array('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201', ),
        ),
        "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    )
);
echo var_dump(file_get_contents($url, false, $context));

Why this code gives the html on localhost and not on the server?为什么这段代码在本地主机上而不是在服务器上给出 html?

Maybe try this to catch the error:也许试试这个来捕捉错误:

set_error_handler(
    function ($severity, $message, $file, $line) {
        throw new ErrorException($message, $severity, $severity, $file, $line);
    }
);

try {
    file_get_contents($url, false, $context));
}
catch (Exception $e) {
    echo $e->getMessage();
}

restore_error_handler();

Source: How can I handle the warning of file_get_contents() function in PHP?资料来源: 如何处理 PHP 中的 file_get_contents() function 的警告?

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

相关问题 file_get_contents 在本地主机上工作,但在服务器上超时 - file_get_contents works on localhost but times out on server PHP:file_get_contents():无法打开流错误-在测试XAMPP测试服务器上有效,无法运行 - PHP: file_get_contents(): failed to open stream error - works on test XAMPP test server, not live php file_get_contents()在测试服务器上有效,但在生产服务器上无效 - php file_get_contents() works on test server but not on production server PHP file_get_contents在本地但在服务器上不起作用 - PHP file_get_contents works on Local but not on server file_get_contents 可以在本地主机上正常工作,而不是在共享主机上 - file_get_contents works right on localhost not on shared hosting PHP file_get_contents服务器检查 - PHP file_get_contents server check php file_get_contents在服务器上不起作用 - php file_get_contents not working on server php file_get_contents在实际服务器上不起作用! 在本地主机上工作? - php file_get_contents not working on real server! does work on localhost? file_get_contents不在localhost上工作,但在在线Web服务器上工作 - file_get_contents not working on localhost but working on online web server PHP + IIS:使用本地主机URL“挂起” file_get_contents - PHP + IIS : file_get_contents “hangs” using a localhost URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM