Help with get request in Json format

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sjuaq
Forum Commoner
Posts: 33
Joined: Thu Aug 10, 2017 4:48 pm

Help with get request in Json format

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with get request in Json format

Post by requinix »

Are you familiar with Javascript? Ever used things like if statements?
sjuaq
Forum Commoner
Posts: 33
Joined: Thu Aug 10, 2017 4:48 pm

Re: Help with get request in Json format

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with get request in Json format

Post 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.
Last edited by requinix on Mon Dec 04, 2017 4:43 am, edited 1 time in total.
Reason: readfile, not passthru. I wouldn't edit but that was a big mistake.
sjuaq
Forum Commoner
Posts: 33
Joined: Thu Aug 10, 2017 4:48 pm

Re: Help with get request in Json format

Post by sjuaq »

Thanks
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Help with get request in Json format

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Help with get request in Json format

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with get request in Json format

Post 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.
Post Reply