-1

I have this Fox Sport API url :

https://newsapi.org/v1/articles?source=fox-sports&sortBy=top&apiKey=955acf3993df49169dfa33dce76d015f

How do i use this? I know it's based on Json. But where do i put this URL? in what format or file? Can someone please help me?

I alrady tried putting it between scripts tags in my index.php file but no results...

Thank you!

user313020
  • 11
  • 6
  • you'll need to use `ajax` to get the data. – Amit Kumar Ghosh Jan 24 '17 at 11:44
  • I used this script but there is no output. How can i solve this? – user313020 Jan 24 '17 at 12:33

3 Answers3

0

Google it ---> Php curl. Read the doc! If you are using a framework, let us know which one

Sphinx117
  • 97
  • 1
  • 5
  • I used this script but there is no output. How can i solve this? – user313020 Jan 24 '17 at 12:34
  • Hoo finally you want to call api from js? Or from php? In your first post you was talking about php right?? – Sphinx117 Jan 25 '17 at 02:59
0

JSON is simply a useful way to send and receive strings that represent objects in JavaScript. In these days I'm working on an application that uses JSON strings to store and retrieve data, and I found a very simple way to get data from JSON strings, that is jQuery. This library has a jQuery.getJSON() method, which let you to load JSON-encoded data from the server (or locally) using a GET HTTP request. Here you can find all the details you need to use this method.

Obviously you could choose not to use any third-part library and do what you need in vanilla JavaScript, but jQuery is very useful since it helps to avoid common cross-browser issues.

In my application I store data from a JSON string in this way:

var placesList;
jQuery.getJSON("places.txt").done(function (data) {
  placesList = data;
});

that is using an external variable to store them using an anonymous function. As you can see, my URL here is places.txt, but you can use any valid URL you want that provide a JSON string.

Pine Code
  • 2,466
  • 3
  • 18
  • 43
  • I used this script but there is no output. How can i solve this? – user313020 Jan 24 '17 at 12:34
  • Using you code I'm able to see the data from the JSON string. Look at the code in your first comment: you didn't specify any `success` function, but only an `error` function. In this way, you aren't doing anything with the data from the server – Pine Code Jan 24 '17 at 13:02
  • can you send me an example of working code please? i'm a real noob at this :( – user313020 Jan 24 '17 at 13:04
  • Sure! Let's create in the body a `
    ` element, where we will print our results. In the `script` tag, we use the `$.ajax()` function with these parameters: `https://newsapi.org/v1/articles?source=the-huffington-post&sortBy=top&apiKey=955acf3993df49169dfa33dce76d015f` for the `url`, `GET` for the `method` (is better that `type`), and finally the two functions, one `success : function(data) { $("#test").html(data.source); }` and the last `error : ...`. In this case, I'm printing the `source` property of the object
    – Pine Code Jan 24 '17 at 13:11
0

You should try like this -

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        var url = 'https://newsapi.org/v1/articles?source=fox-sports&sortBy=top&apiKey=955acf3993df49169dfa33dce76d015f';
        $.getJSON(url).then(function(res){
            console.log(res) //{status: "ok", source: "fox-sports", sortBy: "top", articles: Array[10]}
            console.log(res['status']) //ok
            console.log(res['source']) //fox-sports
            console.log(res['sortBy']) //top
            console.log(res['articles'].length) //10
        })
    })
</script>
Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24