Search code examples
phpjqueryweb-development-serverweb-deployment-project

How to remove the following PHP errors from the website?


<?php

$weather = "";
$error = "";
// if($_GET["city"]){ 
    // better way -->
    if(array_key_exists('city',$_GET))
    {
    $city = str_replace(' ','',$_GET['city']);

    $file_headers = @get_headers("https://www.weather-forecast.com/locations/".$city."/forecasts/latest");

    if($file_headers[0] == 'HTTP/1.1 404 Not Found'){
        $error = "City couldn't be found";
    }
    else{
    $forcastPage = file_get_contents("https://www.weather-forecast.com/locations/".$city."/forecasts/latest");
    
    $pageArray = explode('3 days)</div><p class="b-forecast__table-description-content"><span class="phrase">',$forcastPage);

    if(sizeof($pageArray) >1){
    $secondPageArray = explode('</span></p></td>',$pageArray[1]);
    if(sizeof($secondPageArray)>1){
    $weather =  $secondPageArray[0];
    }
    else{
        $error = "City couldn't be found";
    }
    }
    else{
        $error = "City couldn't be found";
    }
}

    }

?>

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Weather Scraper</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
    <style>
        html{
            background: url(sunset.avif) no-repeat center center fixed;
            background-size: cover;
        }
        body{
            background:none;
        }
        .container{
            text-align:center;
            margin-top: 100px;
            width:450px;
        }
        input{
            margin:20px 0;
        }
        #weather{
            margin-top: 15px;
        }
    </style>
</head>
  <body>
    <div class="container">
        <h1>What's the weather?</h1>
        <form>
  <div class="mb-3">
    <label for="city" class="form-label">Enter the name of a city</label>
    <input type="text" name="city" class="form-control" id="city" placeholder="Eg. London , Tokyo" 
    value = "<?php
    if(array_key_exists('city',$_GET))
    {
     echo $_GET["city"];
    }
     ?>">
    
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form> 
<div id="weather">
    <?php 
    if($weather){
        echo '<div class="alert alert-success" role="alert">
        '.$weather.'
      </div>';
    }
    else if($error){
        echo '<div class="alert alert-danger" role="alert">
        '.$error.'
      </div>';
    }
    ?>
</div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"></script>
  </body>
</html>

When I ran this code on my local server it ran without any errors and showed the output.But I hosted the webiste on 000webhost, website link:https://overprotective-gard.000webhostapp.com/?city=china

The following errors are coming:

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /storage/ssd4/367/20650367/public_html/index.php on line 17

Warning: file_get_contents(): Failed to enable crypto in /storage/ssd4/367/20650367/public_html/index.php on line 17

Warning: file_get_contents(https://www.weather-forecast.com/locations/london/forecasts/latest): failed to open stream: operation failed in /storage/ssd4/367/20650367/public_html/index.php on line 17

Can somebody please look into the code and help me to remove the errors?


Solution

  • Read this OpenSSL changes in PHP 5.6.x

    Try to change in your code like below

    $arrContextOptions = array(
        "ssl" => array(
            "verify_peer" => false,
            "verify_peer_name" => false,
        ),
    );  
    
    $forcastPage = file_get_contents("https://www.weather-forecast.com/locations/".$city."/forecasts/latest", false, stream_context_create($arrContextOptions));