GENWiki

Premier IT Outsourcing and Support Services within the UK

User Tools

Site Tools


php:google_safe_browsing_check_via_api
function GoogleSafeBrowsing($url)
{
    $data = '{
      "client": {
        "clientId": "MyCompany_or_Site_name",
        "clientVersion": "1.0"
      },
      "threatInfo": {
        "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
        "platformTypes":    ["ALL_PLATFORMS"],
        "threatEntryTypes": ["URL"],
        "threatEntries": [
          {"url": "'.$url.'"}
        ]
      }
    }';
    $key='YOUR API KEY';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=".$key);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", 'Content-Length: ' . strlen($data)));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    //curl_setopt($ch, CURLOPT_VERBOSE, true);
    $curldata=curl_exec($ch);
    //$httpcode=curl_getinfo($ch,CURLINFO_HTTP_CODE);
    $response = (array) json_decode($curldata, true);
    return ($response['matches'][0]['threatType']) ? true : false;
}

Simply call GoogleSafeBrowsing('gen.net.uk'); and the function will return TRUE if listed as unsafe, otherwise FALSE. You can of course alter this to return the curl response which is either empty or has JSON data and parse it yourself for more detail.

/data/webs/external/dokuwiki/data/pages/php/google_safe_browsing_check_via_api.txt · Last modified: 2019/05/20 10:17 by genadmin