What is the best practice for integrating external content into my website?

Asked

Viewed 986 times

5

I am developing a site in PHP and I will need to integrate a content that will be on a page of an external site. This page will be with little formatting, without header footer and sidebar, I’ll just have the link from this page.

On this page you will have a product with your thumbnail, name, price and link to see more details.

My doubt and the best way to make this integration. I thought to use iframe but I’m idle on responsiveness and also on site SEO.

There is a better option?

Show 1 more comment

2 answers

2


There is no best way. It all depends on how interconnected these sites are and how much you "trust" them.

Using iframes

The use of iframes, as I described in this other answer, is perfectly valid when you want to insert content that is independent of the current page, either for aesthetic issues, performance or safety.

The problem of using a iframe it is not so much that it is bad in itself, but it brings several other challenges:

  • How to scale the frames and maintain a layout consistent with the rest of the page?
  • What to do with links inside the frame?
  • Is it necessary to interact with frame content? In this case it would be necessary to define access header from different sources.

If the content of this Wordpress site that you want to include is something that occasionally could cause security problems, such as injection scripts on your page, the iframe helps isolate that content.

Also, if the content varies frequently and you can not build a parser, the iframe ends up being the safest alternative.

Getting the content on the server

Using the HTML directly

If, on the other hand, you trust that other site, perhaps because you are the administrator yourself, you can do as Ricardo suggested in the other reply.

Your server makes a request to the other site and retrieves the content from the page you want to insert. Then inserts this content into your page.

In addition to safety, this technique needs to consider:

  • Styles and the structure of the other page. They may not work if there are relative paths or they may interfere with the styles and structure of your page.
  • Making requests to another server each time you show a page will kill your performance. Ideally, keep this information on cache, maybe in a database for a while and update from time to time.

Interpreting the information

If the content always follows the same structure, the most advanced and flexible technique is to make some code to read the content of the other site and extract only the relevant information from each product.

With this information on your server, you can handle it yourself to prevent it from bringing security risk, such as coding it into HTML by writing to the page, limiting the size, etc. Then you can display it in any format you want.

The second part of Ricardo’s answer shows this alternative, using a parser xml. But consider that HTML does not follow the same XML rules, so chances are that you will need to use some specific library to interpret HTML.

Of course, the ideal would be to be able to recover the data via web services in a friendly format like JSON, but parsing HTML breaks a branch when it is not possible.

If you are the administrator of the Wordpress site, consider that there are some plugins to work with REST. If the site uses a plugin to manage products, it is likely that it has some API. Look closely at the documentation before you draw conclusions.

Finally, to reinforce, this technique does not work well if the information read does not have a defined structure.

0

You can use the file_get_contents function of PHP.
This function reads all the contents of a URL and returns to a string.
It would look something like this:

<?php 
    $url = file_get_contents('http://minha_url.com.br');
    var_dump($url);
?>

I hope I’ve helped.

UPDATE

In this case you will need to manipulate the external data with DOM.
It would look something like this:

<?php
    $url = "http://minha_url.com.br";

    $html = trim(file_get_contents($url));
    $dom = new DOMDocument;
    $dom->loadXML($html);
    $dados = $dom->getElementsByTagName('*');
    foreach ($dados as $key => $value) {
       print_r($value);
    }
?>

With this you can get all tags and content.

  • I edited my question to make it clearer. I need the content to come in HTML, with image, link, title, product price, etc.

  • And would this form be better than iframe? How does the issue of responsiveness and SEO work with this function?

  • Wendell, this is a question you must evaluate very carefully. Many developers believe that iframes are not indexed by search engines. This concept is wrong. The iframes are indexed yes, but as it is an external link in the rendering of the searchers the content link goes to the page of that content and not to the master Frameset. In case if you use the above solution will not have this problem as it would be as if you manipulated an object with common content.

  • My opinion I would not use iframe in a project in 2016. If the above solution does not suit you there is an infinite number of possibilities to explore. I hope I’ve helped.

  • In fact, if you are inserting on the site something identical that is on another site is already catastrophic for SEO. If you were really worried about SEO I think you should rethink this before you’re worried about getting it via Curl (or another method like the one shown by Ricardo) or by iframe....

  • @Inkeliz I’m setting up a site where he needs to present only some products from another site, the rest of the content of this site will be authentic, so I care about SEO.

Show 1 more comment

Browser other questions tagged

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