file_get_contents generating 500 error

Asked

Viewed 421 times

-1

I have a file that gives a file_get_contents in archives .
PHP can see the files because the file_exists works just right.
But if I give file_get_contents in the same file as the file_exists PHP returns me:

error 500 (internal server).

<?php 
date_default_timezone_set('America/Sao_Paulo'); $jogosF = 1; $datadehj = date("Y-m-d"); $horaAgora = date("H:i"); $array = array(); $tempo = (10 * 60); while(file_exists("../../../txts/bkpjogosF". $jogosF .".json")) {
$conteudo = file_get_contents("../../../txts/bkpjogosF". $jogosF .".json");
$conteudo = json_decode($conteudo);

foreach ($conteudo->data as $jogosDia) {
    if($jogosDia->odds->data != null) {
        $dataInicio = $jogosDia->starting_date;
        $horaInicio = $jogosDia->starting_time;
        if ($datadehj == $dataInicio && ((strtotime($horaAgora) + $tempo) < strtotime($horaInicio))) {
            $idMatch = $jogosDia->id;
            $timeCasa = $jogosDia->homeTeam->name;
            $escudoCasa = $jogosDia->homeTeam->logo;
            $timeFora = $jogosDia->awayTeam->name;
            $escudoFora = $jogosDia->awayTeam->logo;
            foreach ($jogosDia->odds->data as $oddses) {
                foreach ($oddses->types->data as $joOdds) {
                    if ($joOdds->type == "1x2") {
                        $bookmakerId = $oddses->bookmaker_id;
                        foreach ($joOdds->odds->data as $jogum) {
                            switch ($jogum->label) {
                                case 1:
                                $cotationC = $jogum->value;
                                break;
                                case 2:
                                $cotationF = $jogum->value;
                                break;
                                case "X":
                                $cotationEmp = $jogum->value;
                                break;
                            }
                        }
                        break 2;
                    }
                }
            }
            $datas = new DateTime($dataInicio);
            $horas = new DateTime($horaInicio);
            $horaInicio = $horas->format('H:i');
            $dataInicio = $datas->format('d/m/Y');
            $array[] = array(
            "horaI" => $horaInicio,
            "DataI" => $dataInicio,
            "timeCasa" => $timeCasa,
            "timeFora" => $timeFora,
            "idPartida" => $idMatch,
            "cotTimeC" => $cotationC,
            "cotEmp" => $cotationEmp,
            "cotTimeF" => $cotationF,
            "escudoC" => $escudoCasa,
            "escudoF" => $escudoFora,
            "bookmaker" => $bookmakerId
            );
        }
    }
}
$jogosF++;
fclose($handle); } $sorteador = usort($array, "Meusort"); echo json_encode($array); function Meusort($a, $b) { if($a == $b) { return 0; } return ($a<$b)?-1:1; } ?>

Note: The folder on the server has chmod -R 777 file, ie full permission. I have tried using the fopen, and made the same mistake.
On LOCALHOST with XAMPP it works.

  • 1

    And which error is returned?

  • What version of your PHP on the server? Wouldn’t it be 5.3? Can you download the logs generated by error_log= (php.ini) from php?

  • "GET https://site.com 500 (Internal Server Error)"

  • Possibly unhandled rejection: {"data":"","status":500,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"_classes/gamesBack.php","headers":{"Accept":"application/json, text/Plain, /"}},"statusText":"Internal Server Error"}

  • These errors return me in the console and my php version is 5.0

  • The error that php returns to you, not the request @Krint status code. If you are running in production, try enabling php’s bug report

  • You mean the log? I’m trying to find

  • usually apache saved in /var/logs/, but it helps if you put the following at the beginning of the script: ini_set( 'error_reporting', E_ALL );&#xA; ini_set( 'display_errors', 'On' );&#xA; ini_set( 'track_errors', 'On' );

  • it might have nothing to do with file_get_contents, if it’s enabled in php.ini... then we’ll only know with the error log

  • Opa, returned me the error and I discovered that it was because the memory was exhausted, I searched and put this command line ini_set("memory_limit", "256M"). Now it worked. Thank you!!!

Show 5 more comments

1 answer

0

file_get_contents is a function that depending on how you use may represent a security breach, so the use of it is activated by php.ini

If you use it without enabling it in php.ini it will return this error 500.

To enable change:

allow_url_fopen = 1

But it is HIGHLY recommended to use the CURL instead of this function.

function get_content($URL){
    $ch = curl_init();
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL, $URL);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;
}

Use: echo get_content('http://example.com');

  • this allow_url_fopen is in php.ini or the.php script file? And Curl works when you use it for a file that’s on the same server as the.php script?

  • allow_url_fopen is in php.ini For a file on the same server I would use file_get_contents anyway. And if the file is larger than 1MB I would use file functions. Type fread

  • I used both, and it keeps giving the error 500

  • Ah, I found allow_url_fopen and it was like this: "allow_url_fopen = on"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.