查看完整版本: 關於Safe Browsing APIs
頁: [1]

h4674156 發表於 2017-9-28 01:13 AM

關於Safe Browsing APIs

本帖最後由 h4674156 於 2017-9-28 01:14 AM 編輯

各位前輩好
最近在嘗試這個
但是一直都沒有辦法成功
<?php
$TestUrl=$_GET["url"];
$apikey = "YOUR_API_KEY";

$data='{
        "client":{"clientId": "SafeiZO","clientVersion": "1.0"},
        "threatInfo":{
                "threatTypes":["MALWARE", "SOCIAL_ENGINEERING"],
                "platformTypes":["WINDOWS"],
                "threatEntryTypes":["URL"],
                "threatEntries":[{"url": "'.$TestUrl.'"}]
        }
}';

$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;
}

$GOtest=GETData($url_api, $data);
$str=json_decode($GOtest);
$stats=$str->matches->threatType;
if(isset($stats)){echo"該網頁已受感染";}
?>

url輸入:http://127.0.0.1:8899/safe_api.php?url=https://google.com.tw
測試結果:
Notice: Undefined property: stdClass::$matches in D:\xampp\htdocs\safe_api.php on line 31
Notice: Trying to get property of non-object in D:\xampp\htdocs\safe_api.php on line 31

小弟是從這邊去參考的
https://izo.tw/google-safe-browsing-api/
感覺站長沒有完整給範例Code
想詢問前輩該怎麼修改才能夠測試成功呢?
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

johnwanz 發表於 2017-9-28 09:36 AM

先確認, $apikey = "YOUR_API_KEY"; 已有正確的API KEY?
Safe Browsing APIs (v4) - Get started

再參考, 官網說明,
Safe Browsing Lookup API (v4)

把$GOtest, 跟$str都先顯示出來, 做分析吧. 或許回傳就已經可以發現問題.

一般來說, 外部讀回來的資料, 不是同一個系統的, 應該先做些檢查, 是否為空, 格式符合或是否包含錯誤訊息等....<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>
頁: [1]