1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Log in with php to password protected site

Discussion in 'PHP' started by TemporaryFailure, Jun 12, 2017.

  1. #1
    I know password and username but how I can log in with php? I need that because I need to download documents with cron job.
     
    TemporaryFailure, Jun 12, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Check out cURL-functions.
     
    PoPSiCLe, Jun 12, 2017 IP
  3. TemporaryFailure

    TemporaryFailure Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    
    $username='juuseri';
    $password='salasana';
    $URL='http://www.domain.com/haendler/items.xls';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    $result=curl_exec ($ch);
    echo $result;
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
    curl_close ($ch);
    
    Code (markup):
    Prints to screen file but how I write it to my server? file_put_contents("testi.xls", fopen($result, 'r')); doesn't work. It makes only 0kb file.
     
    TemporaryFailure, Jun 12, 2017 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    You do file_put_contents("filename.ext",$result); why in the world do you have fopen in there? Also, this would show up if you turn on displaying errors.
     
    PoPSiCLe, Jun 14, 2017 IP
  5. TemporaryFailure

    TemporaryFailure Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Yes I screwed up that. But I have error_reporting(E_ALL); in php file and I get never any errors, just blank screen.
     
    TemporaryFailure, Jun 14, 2017 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    That just sends all errors to the log-file. Check that. If you wanna display those errors on screen, use
    
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    
    Code (markup):
    However, this will NOT necessarily show anything on screen - depending on what type of problem, it might not actually reach the browser (500-errors will normally not display anything, since it's a server-error, preventing output)
     
    PoPSiCLe, Jun 17, 2017 IP