How to change content on a page?

8

I'm trying to change some fields of a website, what I want is to open this site in my address, but with some of its content changed.

I'm using file_get_contents to open the site, but I have two problems:

  • When I click on a link within a site, it is redirected to Original URL.
  • I can not make a perfect 'clone' because the site has a lot of content within your .js and css that are accessor internally, example: background: url(.../.../s/images/ui/2011/cards-bg-RN.png) 0 1px no-repeat; what you are looking for in the root of the original site.
  • I would like to know if you can use cURL or another variable to open the site and manipulate certain fields, without having to save all the .css and .js files in the site.

    ps; I am studying PHP at 3 months, and this is a project to present in my course, but I have already tried it in all the ways I know and can not do it correctly using a Regex.

    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: foo=bar\r\n"
      )
    );
    
    $context = stream_context_create($opts);
    
    ;
    $file = file_get_contents('https://urldosite.com/', false, $context);
    echo $file
    
    Okay, I made the page open, but it opens the wrong one because it looks for the .css inside my server, not the source server. My main question is this: would I have to use file_get or cURL to open the site without it looking for .css and .js on my server? as if it were a frame? To manipulate the content of the site I can try to turn myself alone!

        
    asked by anonymous 21.02.2015 / 23:47

    1 answer

    1

    The quick answer is: Yes, you can point to the source server's CSS without having to download them.

    You will need to change the downloaded HTML to point to the server, simple like this, eg

    de

    <html>
       <link rel="stylesheet" href="estilo.css" />
    ..
    

    for

    <html>
       <link rel="stylesheet" href="http://sitedeorigem.com/estilo.css" />
    ..
    

    Notice that what you have to do is to generate an API that places the domain where the file is located.

    Unfortunately I am without an internet to mount in PHP but the algorithm would be something like:

    • Generate an array with existing CSS paths
    • Place the domain path (or not) based on whether or not it already has a domain set

    • Use an HTML parser api (such as PHP Simple HTML DOM Parser ) to grab the CSS links using the link[rel=stylesheet]

    After that the java code that would make the replacement in Regex would look something like:

    String[] urls = new String[]{"estilo.css", "http://google.com.br/style.css"};
    
    for(int i=0; i < urls.length; i++){
        if(!urls[i].matches("^http.*")){
            urls[i] = "http://siteorigem.com/" + urls[i];
        }
        System.out.println(urls[i]);
    }
    

    If I am not mistaken, the PHP function equivalent to matches is preg_match

    Otherwise, I do not know the right solution.

        
    24.02.2015 / 20:40