HTTP-AUTH and cURL

Discussion in 'PHP' started by cridenour, Feb 4, 2008.

  1. #1
    I am using the following code to access a site that is using basic HTTP-AUTH.

    	$ch = curl_init($url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	if($user && $pass){
    	   curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
               curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    	}
    	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30 );
    	curl_setopt($ch, CURLOPT_TIMEOUT,5 );
            $response = curl_exec($ch);
    PHP:
    All the variables are correct. If I access a page without HTTP-AUTH it works perfectly.

    Any input is appreciated. Thanks.
     
    cridenour, Feb 4, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    jayshah, Feb 4, 2008 IP
  3. cridenour

    cridenour Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the link. I made my test code cURL and cURL only and started to find my problem.

    cURL has yet to work - I had just coded a failsafe use of file_get_contents if cURL didn't work.

    I am working behind a proxy, but my proxy settings don't effect anything. Even trying to pull down contents from (http)localhost/folder/file.txt don't even work.

    Also - when I include curl_setopt(CURLOPT_USERPWD, 'user:password') the script will not complete. When commented out, the script gets to the end and tells me it failed.
     
    cridenour, Feb 4, 2008 IP
  4. cridenour

    cridenour Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    (http) = http:// -- not allowed to link yet.
     
    cridenour, Feb 4, 2008 IP
  5. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Have you tried:

    
    $data = file_get_contents("http://username:password@www.thesite.com/thefile.php");
    echo $data;
    
    PHP:
    Or using http://www.php.net/fsockopen (using the "Authorization: Basic [base64-data-here]" header)?

    Jay
     
    jayshah, Feb 4, 2008 IP
  6. cridenour

    cridenour Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    user:pass@ did the trick. Funny I didn't think of that :eek:

    Thanks!
     
    cridenour, Feb 4, 2008 IP
  7. Bilal.G

    Bilal.G Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hello, Im stuck with the same problem.
    But the thing is, I cant use user:pass@server.com because my script is so huge and it connects to different server and each server uses different type of auth..

    Im facing the same problem.
    When I use this:

    curl_setopt($ch, CURLOPT_URL, "http://$user:$pass@server.com");
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    // curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    // curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$pass);

    It works perfectly

    But when I uncomment the auth parameters .. It just fails :s (doesnt matter if I include user:pass@ or not.. it fails everytime)
     
    Bilal.G, Feb 25, 2010 IP