简体   繁体   English

PHP:空结果-file_get_contents

[英]PHP: Empty result - file_get_contents

i am using file_get_contents to run a php script on my website that will export some data in a textfile. 我正在使用file_get_contents在我的网站上运行php脚本,该脚本将在文本文件中导出一些数据。 The php script "export.php" prints out "OK" when the job is done and "Error" if an error occures. 完成作业后,PHP脚本“ export.php”将输出“ OK”,如果发生错误,则将输出“ Error”。

If i run export.php in my browser it will print "OK" if i run it by calling file_get_contents in another php script the result is empty. 如果我在浏览器中运行export.php,如果我通过在另一个php脚本中调用file_get_contents来运行它,则会显示“ OK”,结果为空。 But export.php prints in any case "OK" or "Error". 但是在任何情况下export.php都将输出“ OK”或“ Error”。

This is my script that calls export.php: 这是我的脚本,调用export.php:

$opts = array('http' =>
                array(
                    'method'  => 'GET',
                    'timeout' => 240 
                )
            );

            $context  = stream_context_create($opts);
            $url_pre = "http://";
            $url = "127.0.0.1/export.php";
            $html = @file_get_contents($url_pre.$url,false,$context);
            if ($html === false){
                $job_ok=false;
                $result=error_get_last();
                echo "Error: ".$result."<br />";
            }else{
                if (substr($html,0,2)=="OK"){
                    $job_ok=true;
                    $errurl = "";
                    $result="Job done";
                    echo "Job done: ".$result."<br />";
                }else{
                    $job_ok=false;
                    $errurl = $url_pre.$url;
                    $result=$html;
                    echo "Error: ".$result."<br />";
                }
            }
            echo "HTML: $html <br />";

The result of the script is: 该脚本的结果是:

Error: 错误:

If i open " http://127.0.0.1/export.php " in my browser i get: 如果我在浏览器中打开“ http://127.0.0.1/export.php ”, 则会得到:

OK

Maybe someone can help me! 也许有人可以帮助我!

Thanks! 谢谢!

I have changed file_get_contents to curl and now it works! 我已将file_get_contents更改为curl,现在可以使用了!

function curl_file_get_contents($url){
 $curl = curl_init();
 $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';

 curl_setopt($curl,CURLOPT_URL,$url); //The URL to fetch. This can also be set when initializing a session with curl_init().
 curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
 curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5); //The number of seconds to wait while trying to connect.  

 curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request.
 curl_setopt($curl, CURLOPT_FAILONERROR, TRUE); //To fail silently if the HTTP code returned is greater than or equal to 400.
 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header.
 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
 curl_setopt($curl, CURLOPT_TIMEOUT, 240); //The maximum number of seconds to allow cURL functions to execute.  

 $contents = curl_exec($curl);
 curl_close($curl);
 return $contents;
}

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

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