First of all I am not a programmer. I am starting small project to kill time. I am using Google Safe Browsing API v4 in PHP to get the deceptive sites which contain malware. I searched on Google and was able to get what I want. But if I turn on error reporting, I get the following error:Notice: Undefined property: stdClass::$matches in /var/www/html/working.php on line 55  Notice: Trying to get property of non-object in /var/www/html/working.php on line 55The cause of the error is because, if a site is not listed as a deceptive site, then it will respond with an empty JSON. Due to this, the following line will not execute and will throw that error:$stats=$str->matches[0]->threatType;This PHP script will read the sites.txt file which contains website names per line and lists sites according to Google Safe Browsing. I want to handle this error. Please review this script and suggest ways to minimize the script, execution time, etc.<html><head>        <style>                *{padding: 0; margin: 0}                #wrap{ padding: 0 10px; width: 620px }                .left{float: left; width: 305px}                .right{float: right; width: 305px}                .warning{background: red;color: white; padding: 0 5px}                .ok{background: green;color: white; padding: 0 5px}                .clear{clear: both}        </style></head><body><?phpini_set('display_errors', 1);ini_set('display_startup_errors', 1);error_reporting(E_ALL);$sites = fopen(sites.txt, r);$apikey = GOOGLE-API-KEY;$url_api =https://safebrowsing.googleapis.com/v4/threatMatches:find?key=.$apikey.;function GETData($url, $post){ $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array(Content-Type: application/json, 'Content-Length: ' . strlen($post))); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result=curl_exec($ch); return $result;}echo '<div id=wrap>';if($sites){        while (($line = fgets($sites)) !== false){                $data=                '{                  client: {                  clientId: TestClient,                  clientVersion: 1.0                  },                  threatInfo: {                  threatTypes:      [MALWARE, SOCIAL_ENGINEERING],                  platformTypes:    [LINUX],                  threatEntryTypes: [URL],                  threatEntries: [                   {url: '.$line.'}    ]  }}';$GOtest=GETData($url_api, $data);$str=json_decode($GOtest);$stats=$str->matches[0]->threatType;if(isset($stats)){        echo '<div class=left>' . $line . '</div>';        echo '<div class=right>';        echo '<span class=warning>' . $stats . <br> . '</span></div>';        echo '<div class=clear></div>';}else{        echo '<div class=left>' . $line . '</div>';        echo '<div class=right>';         echo '<span class=ok>' . Looks OK. <br> . '</span></div>';        echo '<div class=clear></div>';}}fclose($sites); echo '</div>';}?></body></html>
Getting deceptive/malware content sites list with Google Safe Browsing v4
php;api
Heredoc                $data=                '{                  client: {                  clientId: TestClient,                  clientVersion: 1.0                  },                  threatInfo: {                  threatTypes:      [MALWARE, SOCIAL_ENGINEERING],                  platformTypes:    [LINUX],                  threatEntryTypes: [URL],                  threatEntries: [                   {url: '.$line.'}    ]  }}';It's more common to use a heredoc and string interpolation for a multiline string like this.                  $data = <<<EOJSON{    client: {     clientId: TestClient,     clientVersion: 1.0    },    threatInfo: {     threatTypes:      [MALWARE, SOCIAL_ENGINEERING],     platformTypes:    [LINUX],     threatEntryTypes: [URL],     threatEntries:    [      {url: $line}     ]  }}EOJSON;Simplifying$stats=$str->matches[0]->threatType;if(isset($stats)){        echo '<div class=left>' . $line . '</div>';        echo '<div class=right>';        echo '<span class=warning>' . $stats . <br> . '</span></div>';        echo '<div class=clear></div>';}else{        echo '<div class=left>' . $line . '</div>';        echo '<div class=right>';         echo '<span class=ok>' . Looks OK. <br> . '</span></div>';        echo '<div class=clear></div>';}You can rewrite this     echo '<div class=left>' . $line . '</div>';    echo '<div class=right>';    if (property_exists($str, 'matches')) {        echo '<span class=warning>' . $str->matches[0]->threatType . '<br></span></div>';    }    else {        echo '<span class=ok>Looks OK<br></span></div>';    }    echo '<div class=clear></div>';This saves repeating three lines that are identical.  Only the one line that changes is inside the if.  I also compressed the strings.  PHP doesn't automatically merge string literals, so this might make some small performance difference.  