How to change the host address where the "src" and "href" requests go? [closed]

0

Example: I give file_get_contents('http://youtube.com') and then, if the src and href of the html do not have the entire path, but only src="/pasta/arquivo.ext" , instead of src="https://youtube.com/pasta/arquivo.ext" , the requests will give all 404 not found , because it will fetch on my server (localhost).

Is there any parameter of header http that I can change to indicate where I want those requests to go? I tried to rewrite and put the entire path in the references, by means of str_replace() , but no use, because the javascript files make request for the localhost and it disrupts the operation in the same way. You can not download everything and rewrite; I wanted to change this in the http header (I think it's possible)

YouTube was just the example level. I do not want to copy the site but to make modifications in the css of the embed player. (I do not want libs, I'm doing this as an exercise)

Thank you all.

    
asked by anonymous 27.06.2017 / 05:49

1 answer

2

The right term would be to "turn relative url into absolute url".

Verify that the URL has http at the beginning of the string. If you do not, it's probably a relative URL.

$domain = 'https://dominio/';
$url = '/pasta/arquivo.txt';

$url = ltrim($url, '/'); //remove barra inicial, caso exista.
if (substr($url, 0, 4) != 'http') {
    $url = $domain.$str; // concatena ao domínio
}

echo $url;

If you want, add more consistency as it is only checked if there is "http" at the beginning of the string but if you find an absolute URL with protocol other than "http" as "ftp: //" for example, it will have an invalid URL.

One technique is to check if within the first 12 characters it contains :// .

If there is :// and start is different from http , ignore the entire URL or take another action as desired.

    
27.06.2017 / 07:16