Results 1 to 8 of 8

Thread: How to copy remote html to localhost folder ?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    How to copy remote html to localhost folder ?

    Hi all. could any one show me how i can copy a remote html page to a folder inside localhost folder. For example i want to copy the following url html to folder named copy and name it to whatever value of $id is plus .html (example :23434.html) :


    remote URL to copy :
    PHP Code:
    $copyfileurl="http://friends.myspace.com/index.cfm?fuseaction=user.viewfriends2&friendID="$id
    Later i want to load the copied html to curl_setopt($curl, CURLOPT_URL, '.....html'


    PHP Code:
    curl_setopt($curlCURLOPT_URL'.....html');
    curl_setopt($curlCURLOPT_POST0);
    $result curl_exec($curl); 
    I hope some one show me how this can be then.Thanks

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: How to copy remote html to localhost folder ?

    Well providing you have permissions it's as simple as this:
    PHP Code:
    $copyfileurl="http://friends.myspace.com/index.cfm?fuseaction=user.viewfriends2&friendID="$id;
    file_put_contents($id.".html"file_get_contents($copyfileurl)); 
    I don't know what you mean by loading it into cURL. But I guess it would just be 'http://localhost/'.$id.'.html'.
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to copy remote html to localhost folder ?

    Thanks rudi. What i mean by loading is to use the same saved html file inside this function curl_setopt instead of test.html .
    PHP Code:
    curl_setopt($curlCURLOPT_URL'test.html' 
    I tried this but it value of url didn't get loaded properly !!


    PHP Code:
            curl_setopt($curlCURLOPT_URL'http://localhost/testFolder/'.$id.'.html.'); 
    Furthermore, is it possible to save the remove file inside a folder instead of current directory where this script runs ?

  4. #4
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: How to copy remote html to localhost folder ?

    Writing:
    PHP Code:
    $copyfileurl="http://friends.myspace.com/index.cfm?fuseaction=user.viewfriends2&friendID="$id
    file_put_contents('./testFolder/$id.".html", file_get_contents($copyfileurl)); 
    Reading:
    PHP Code:
    curl_setopt($curlCURLOPT_URL'http://localhost/testFolder/'.$id.'.html'); 
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to copy remote html to localhost folder ?

    I think i got the problem. The following code works if i save the remote page to localhost folder manually!! otherwise it will not work.

    i saved the remote file manully and used the following code and boom it gave me the page number .


    I think copying the remote file and processing it at same time will not work as the preg_match function has not enough time to process it. Is there any way to put some sort of delay so that preg_match fires ONLY after the remote page copied completely to local drive?
    PHP Code:
    if (preg_match('/of <a href="([^"]+)" class="pagingLink">(\d+)<\/a>/'$result$matches))
        {
            
    $pages $matches[2];
                echo 
    " Number of pages: ".$pages." ";
                unset(
    $matches);

        } 

    PHP Code:
    $copyfileurl="http://friends.myspace.com/index.cfm?fuseaction=user.viewfriends2&friendID="$id
    file_put_contents($id.".html"file_get_contents($copyfileurl));

    // Retrieve friends page.
        
    echo 'Checking friends list... ';
            echo 
    " <br>";
           
            
    curl_setopt($curlCURLOPT_URL'http://localhost/test/'.$id.'.html');
        
    curl_setopt($curlCURLOPT_POST0);
        
    $result curl_exec($curl);

     
    // i added this 
    //print $result;
        
    $headers curl_getinfo($curl);
        
        
    // Find out how many pages there are. 

        
    if (preg_match('/of <a href="([^"]+)" class="pagingLink">(\d+)<\/a>/'$result$matches))
        {
            
    $pages $matches[2];
                echo 
    " Number of pages: ".$pages." ";
                unset(
    $matches);

        }
    else
    {

    echo 
    "couldn't get the number of pages";



  6. #6
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: How to copy remote html to localhost folder ?

    Well you can sleep(int seconds); but that's not practical.

    Anyway, file_get_contents should halt execution until it's complete anyway, so I'm not sure what you mean, meaning I don't think that's your issue.

    Have you tried updating PHP/Apache as per your other post??

    I don't see why you're saving it to disk when you could just grab the contents straight into a string and preg match on the string. Using cURL for a simple GET request seems like overkill to me too.
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to copy remote html to localhost folder ?

    well i can't update my server at this time as i am worried that my other
    program stops working. But any ways after testing many things. i found out that my preg match function prints out the result only if the html file to process is saved localy with .html extention. Otherwise it will not work for some reasons that i don't know why. That is why i decided to save the remote page first them process it!!

    I was thinking that preg_match function gets excuted in the middle or just before end of the copy process.

    PHP Code:
    if (preg_match('/of <a href="([^"]+)" class="pagingLink">(\d+)<\/a>/'$result$matches)) 
        { 
            
    $pages $matches[2]; 
                echo 
    " Number of pages: ".$pages." "
                unset(
    $matches); 

        } 

  8. #8
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: How to copy remote html to localhost folder ?

    Like I said, file_get_contents will block until it is done downloading, and file_put_contents will block until it's done writing too.

    It might be worth setting up an upgraded system to run on port 81 (for example) to test if the upgrades have any effect on what you're doing since what you are describing is not happening on my 2 local machines nor either of my servers.
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width