1

I'm trying validate url for comment form.

Then I thought ”Safe Browsing Lookup API” looked good.

However, the following code only returns an empty array in $result.

What can I do to improve this code?

code:

<?php
$api_key = 'xxx';
$url = 'https://safebrowsing.googleapis.com/v4/threatMatches:find?key=' . $api_key;
$data = [
    "client"=>[
      "clientId"      => "yourcompanyname",
      "clientVersion" => "1.5.2"
    ],
    "threatInfo"=>[
      "threatTypes"     =>["MALWARE", "SOCIAL_ENGINEERING"],
      "platformTypes"   =>["WINDOWS"],
      "threatEntryTypes"=>["URL"],
      "threatEntries"   =>[
        ["url"=>"http://goooogleadsence.biz/"],
        ["url"=>"http://www.urltocheck2.org/"],
        ["url"=>"http://activefile.ucoz.com/"]
      ]
    ]
];
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
var_dump($result); // -> string(3) "{}"
curl_close($ch);
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
chagean
  • 11
  • 1
  • Maybe simply none of your URLs are on any of the lists to begin with? Quote documentation you linked to - _“If there are no matches (that is, if none of the URLs specified in the request are found on any of the lists specified in a request), the HTTP POST response simply returns an empty object in the response body.”_ – CBroe Feb 24 '20 at 12:33
  • @CBroe I think those URLs are specified.There was listed here→https://stackoverflow.com/questions/41249657/google-safe-browsing-api-v4-empty-response – chagean Feb 24 '20 at 12:43
  • The JSON data you are creating has the slashes in the URL values escaped, whereas the examples from the documentation don’t - not sure if that makes a difference. Try and encode your data with the `JSON_UNESCAPED_SLASHES` flag set, and see if that makes any difference. – CBroe Feb 24 '20 at 12:53
  • I see. 「$data_json = json_encode($data,JSON_UNESCAPED_SLASHES);」 Unfortunately, nothing's changed much since last time. – chagean Feb 24 '20 at 13:28

0 Answers0