1

i am using file_get_contents to run a php script on my website that will export some data in a textfile. The php script "export.php" prints out "OK" when the job is done and "Error" if an error occures.

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. But export.php prints in any case "OK" or "Error".

This is my script that calls 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:

OK

Maybe someone can help me!

Thanks!

3
  • I don't see anything bad. I never called local files through IP though. Tried to use 'localhost' instead of IP? Commented Jan 16, 2015 at 9:28
  • 3
    remove @ from @file_get_contents to see if function is returning errors Commented Jan 16, 2015 at 9:33
  • i removed the @ from file_get_contents and did not get any error. i also tried localhost but wont work. Maybe the script get any sort of timeout. Because loading the page in the browser will took about 60 seconds. Commented Jan 16, 2015 at 10:52

1 Answer 1

1

I have changed file_get_contents to curl and now it works!

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.