Page 1 of 1
Help with get request in Json format
Posted: Thu Nov 30, 2017 5:43 am
by sjuaq
Hi,
I'm trying to implement a feature in a program but i'm having troubles fetching the JSON url.
The url is
http://api.stopforumspam.org/api?json&e ... l@here.com
And the code received should be something like this (the "var json" in this case is just an example)
Code: Select all
var json = '{"success":1,"email":{"frequency":1,"appears":1,"confidence":0.03}}',
obj = JSON && JSON.parse(json) || $.parseJSON(json);
document.write('Is a Spammer? '+obj.email.appears+'<br>');
document.write('Last Seen: '+obj.email.lastseen+'');
Also is it possible to change 1 or 0 in obj.email.appears to yes or no?
Thanks
Re: Help with get request in Json format
Posted: Thu Nov 30, 2017 6:20 am
by requinix
Are you familiar with Javascript? Ever used things like if statements?
Re: Help with get request in Json format
Posted: Thu Nov 30, 2017 11:52 am
by sjuaq
Yes, i know how IF statement works i just couldn't remember how.
I have 3 alternatives to run the api but only the first one does make an actual valid request but doesn't retrieve any information or may be misconfigured.
Requesting to 127.0.0.1 works but not in this url
Code: Select all
<html>
Is Spammer? <a id='demo'></a><br>
Last seen: <a id='query'></a>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.email.appears;
document.getElementById("query").innerHTML = myObj.email.lastseen;
}
};
xmlhttp.open("GET", "https://api.stopforumspam.org/api?json&email=email@hotmail.com", true);
xmlhttp.send();
</script>
</html>
Requesting to 127.0.0.1 doesn't work nor the alert popup
Code: Select all
// requires jQuery
<script>
var json = (function () {
$.ajax({
type: "GET"
url: https://api.stopforumspam.org/api?json&email=email@hotmail.com,
dataType: "json",
success: function (data) {
json = data;
}
});
return json;
})();
</script>
<script>
alert(json);
</script>
Request to 127.0.0.1 doesn't work but the code has worked in another project
Code: Select all
// requires jQuery
function isspammer(email) {
var API_Url = "https://api.stopforumspam.org/api?json&email=";
$.get(API_Url + email, function(jsonObj){
if (jsonObj.length > 0) {
var list = "The list:<br><br>";
jsonObj.forEach(function(element) {
list += element + "<br>";
});
document.write(list);
}
$(document).ready(function(){
isspammer("email@hotmail.com");
});
}
Any help will be great, probably i'm just missing something...
Thanks
Re: Help with get request in Json format
Posted: Fri Dec 01, 2017 1:48 am
by requinix
You can't do a JSON request to their site.
Make a PHP script on your site
Code: Select all
<?php
if (isset($_GET["email"])) {
header("Content-Type: application/json");
readfile("https://api.stopforumspam.org/api?json&email=" . urlencode($_GET["email"]));
} else {
http_response_code(400);
}
then call that (with the email) in your AJAX.
Re: Help with get request in Json format
Posted: Fri Dec 01, 2017 2:43 pm
by sjuaq
Thanks
Re: Help with get request in Json format
Posted: Mon Dec 04, 2017 4:15 am
by VladSun
Huh,
passthru() ? Isn't it supposed to have a local command as a string parameter? I doubt it has a
network protocol wrapper.
And I strongly advice people to use
cURL for getting remote content, instead of relying on unsafe file open calls.
Re: Help with get request in Json format
Posted: Mon Dec 04, 2017 4:38 am
by VladSun
Also there are several ways to execute crossdomain AJAX calls, and one of them - JSONP is supported by the service provider you use:
JSON Padding
We provide ajax/jquery support with JSONP, which allows for a callback function to be specified around a json result. If no callback function is provided then a standard json response will be provided.
http://api.stopforumspam.org/api?email= ... myFunction
myFunction({"success":1,"email":{"lastseen":"2016-08-28 16:08:09","frequency":255,"appears":1,"confidence":99.95}})
This call will return a mime type of text/javascript
Based on a return of "yes", you can choose to deny registration. The lookup is case insensitive, too. Note that you will get an error message if your IP address or email address is improperly formatted.
See the section below for a more reliable method of scoring queries.
https://www.stopforumspam.com/usage
Re: Help with get request in Json format
Posted: Mon Dec 04, 2017 4:42 am
by requinix
VladSun wrote:Huh, passthru() ?
Yeah, wrong function. I meant readfile().
VladSun wrote:Also there are several ways to execute crossdomain AJAX calls, and one of them - JSONP is supported by the service provider you use:
Good call. I had checked for cross-origin headers but didn't think to look for JSONP support.