0

Possible Duplicate:
How do I make a request using HTTP basic authentication with PHP curl?

I am using a PHP Curl Class from http://query7.com/php-curl and need to get content from one thrid party service provider with Basic Authentication.

I have no idea how I can pass my authentication details (i.e. username and password) to the above specified curl class.

Community
  • 1
  • 1
Prakash
  • 2,749
  • 4
  • 33
  • 43

2 Answers2

2

Add the last line to your options array:

public $options = array(
    CURLOPT_RETURNTRANSFER => true,     // return the web page
    CURLOPT_HEADER         => false,    // don't return the headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_USERAGENT      => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)", // set a normal looking useragent
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout
    CURLOPT_TIMEOUT        => 120,      // timeout
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    CURLOPT_USERPWD        => $username . ":" . $password // Basic auth
);
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Yes, but how can I pass that curl options into specified php class. I see the class is using a public variable called `$options`. – Prakash Jul 05 '12 at 01:30
  • the problem is that I can not add the curl option which you have suggested directly to the class, it is because I am using the same class for other purposes (i.e. fetching the pages that do not require authentication) – Prakash Jul 05 '12 at 01:33
  • Then you need to alter the class to allow you to add that optional parameter – John Conde Jul 05 '12 at 01:35
0

Generally you can pass the username and password to the domain like this:

http://username:password@query7.com/php-curl
badcircle
  • 72
  • 3
  • wow, although I need to change my url and point it to actual service URL but this works perfectly – Prakash Jul 05 '12 at 01:39