Curl login to https

Discussion in 'PHP' started by php_techy, Jun 21, 2009.

  1. #1
    Hi,
    Please excuse if similar issue has been posted earlier.
    My scenario is
    Login to a https site .
    Once I login,I find a link to download a file.
    Now all this I want to do with the help of curl.
    I want the script for
    1) login to https site
    2)download the file?After applying the regex,I can get the link of the file easily but problem comes for downloading the file?Do I have to use JavaScript for that or use location header to redirect to file download link?
    Thanks in advance
     
    php_techy, Jun 21, 2009 IP
  2. adfly

    adfly Active Member

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    88
    #2
    Make sure you have OpenSSL and Curl installed.

    <?php
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    
    $postfields="username=xxxx&password=xxxxx";
    
    curl_setopt($ch, CURLOPT_URL,"http://www.example.com");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postfields);
    
    $result = curl_exec ($ch);
    
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_URL,"http://www.example.com/file/to/download.txt");
    
    $result = curl_exec ($ch);
    
    $contents = $result;
    
    $handle=fopen("download.txt", 'w');
    fwrite($handle, $contents);
    fclose($handle);
    
    
    Code (markup):
     
    adfly, Jun 24, 2009 IP