Home / PHP / oEmbed API system using PHP

oEmbed API system using PHP

The oembed system is trending in the field of Open API technology and will be trending along with Web and App Development. This is a very powerful concept with minimal coding effort on the development side. Often referred to as oEmbed system this method provides useful information to the developer with just a simple request. The response given is usually in the form of raw Array, JSON or XML.

oEmbed with PHP?

Its simple. We will be using PHP to get information by streaming to the API server to make the application more realistic!

There are many ways in which you can achieve the target which is to fetch required data from the response. In this tutorial we will be looking into Youtube, Soundcloud and Flickr to show an example of a video, audio and a photo. Check out oEmbed to grab a brief idea about the Request URL for each provider. Let us now see the Request URL Parameters for each mentioned. Every request will contain three parts in the URL,

URL : The provider of the API (Ex: http://www.youtube.com/oembed)

Parameters:

  • url: Link of the video
  • format: Denotes the format in which you want the response. Here you can use either json or xml

Youtube

First you need to send a request to the youtube’s oembed URL. We will be using the following URL

PHP

Explanation

We first set the request URL in the $url variable. You can notice that I have encoded the video link using rawurlencode so as to escape errors. Try removing it to see what error occurs. Next form an array of options to be sent while streaming for the API response, for detailed information see HTTP Stream.

Having set up the stream let us now try to open the video with fopen(). If fopen() fails to read the URL given it will throw an error. r denotes that we only need to read the URL. Now apply stream_get_contents() on fopen() and pull all data coming from the stream as a response. At this point you have all data received and ready to access the video. There is one last step! Just convert the response JSON into PHP array to access the objects. json_decode the received data and store it in a variable.

HTML

Be sure to use $data->html which will get only the html object from the response which contains the required iframe element.

For flickr and soundcloud follow the same method. You can check all three example in the preview page. I hope this tutorial would be helpful for beginners out there 🙂

Download   Preview

About Ashik

I am a Full Stack Developer and love to work on APIs and Apps. Hardcore lover of Ionic and Laravel <3

Check Also

Weather forecast API using jQuery, Ajax and PHP

Weather forecast API is widely used as widgets in small blogs to huge news website …

Do you wan't to get notified?

I will not spam your inbox. I will only send email whenever I publish a new article or to share information about technology

You have Successfully Subscribed!