Retrieve data/text from other websites with PHP

Asked

Viewed 176 times

1

It has to recover data from other sites with php?

Like, I liked to make a streaming site and wanted the ratings of the video (or something) to be constantly updated in relation to imbd.

2 answers

5


Yes, usually these sites create Apis for other sites to pick up information by using Web Services.

I found this API that returns the rating of a series or movie:

http://www.omdbapi.com/

Usually return a JSON (Javascript Object Notation) and in PHP we use the json_encode($object) and json_decode($json) methods to manipulate JSON.

Example:

Link used:

 http://www.omdbapi.com/?t=Game%20of%20Thrones&Season=1&Episode=1

JSON returned:

{"Title":"Winter Is Coming","Year":"2011","Rated":"TV-MA","Released":"17 Apr 2011","Season":"1","Episode":"1","Runtime":"62 min","Genre":"Adventure, Drama, Fantasy","Director":"Timothy Van Patten","Writer":"David Benioff (created by), D.B. Weiss (created by), George R.R. Martin ( "A Song of Ice and Fire " by), David Benioff, D.B. Weiss","Actors":"Sean Bean, Mark Addy, Nikolaj Coster-Waldau, Michelle Fairley","Plot":"Jon Arryn, the Hand of the King, is Dead. King Robert Baratheon plans to Ask his oldest Friend, Eddard Stark, to take Jon’s place. Across the sea, Viserys Targaryen plans to Wed his Sister to a nomadic Warlord in exchange for an Army." "Language":"English","Country":"USA","Awards":"N/A","Poster":"http://ia.media-imdb.com/images/MV5BMTk5MDU3OTkzMF5BMl5BanBnXkFtZTcwOTc0ODg5NA@@. _V1_SX300.jpg","Metascore":"N/A","imdbRating":"8.4","imdbVotes":"13098","imdbID":"tt1480055","seriesID":"tt0944947","Type":"Episode","Response":"True"}

PHP:

<?php

    /* Retorna um JSON de String a partir do método file_get_contents 
    e decodifica esta String de JSON a partir do método json_decode para manipular no PHP. */

    $json = json_decode(file_get_contents('http://www.omdbapi.com/?t=Game%20of%20Thrones&Season=1&Episode=1'));

?>

0

Direct access to the data of a website that does not have an API or web service is very difficult (and could even be fraud). @Giancarlo mentioned a very good solution, being this access to an API.

Many sites have or give access to data with an API. There are usually directions, tutorials, and documentation on how to access these Apis, and the information they will form.

  • 1

    Yes, I was able to do what I wanted for what @Giancarlo told me :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.